正确的方法来生成一个http请求来镜像来自android的api服务

时间:2013-12-20 13:31:32

标签: android google-glass google-mirror-api

我尝试从我的Android应用程序将时间轴卡推送到Glass。我使用这段代码:

String BASE_URL = "https://www.googleapis.com/upload/mirror/v1/";

    try {
        final HttpPost request = new HttpPost();
        request.setURI(new URI(BASE_URL + "timeline"));
        request.addHeader("Authorization", String.format("Bearer %s", token));
        request.addHeader("Content-Type", "application/json");
        request.setEntity(new StringEntity(json.toString()));

        // Execute the request on a background thread
        mThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    final HttpResponse response = mClient.execute(request);
                    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                callback.onSuccess(response);
                            }
                        });
                    } else {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                callback.onFailure(response, null);
                            }
                        });
                    }
                } catch (final IOException e) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onFailure(null, e);
                        }
                    });
                }
            }
        });
    } catch (UnsupportedEncodingException e) {
        // Note: This should never happen
    } catch (URISyntaxException e) {
        // Note: This should never happen
    }

json是这样的:

{"notification":{"level":"DEFAULT"},"text":"Pizza and spaghetti"}

但服务响应时出错:

"error": {
"errors": [
{
"domain": "global",
"reason": "badContent",
"message": "Media type 'application/json' is not supported. Valid media types: [image/*, audio/*, video/*]"
}
],
"code": 400,
"message": "Media type 'application/json' is not supported. Valid media types: [image/*, audio/*, video/*]"
}
}

我按照此处的代码:https://github.com/twaddington/mirror-quickstart-android

任何想法?

聚苯乙烯。还有另一种方法将时间轴卡从Android应用程序推送到谷歌眼镜?

由于

1 个答案:

答案 0 :(得分:2)

正如您在Google文档(https://developers.google.com/glass/v1/reference/timeline/insert)中看到的,此请求仅接受多媒体数据为“内容类型”。

尝试使用正确的内容类型更改此行(图片/ ,音频/ ,视频/ *): request.addHeader("Content-Type", "application/json");

您必须在HTTPost对象中添加多媒体对象。更多信息: Sending images using Http Post

- EDIT-- 您要执行的请求(仅限元数据,没有多媒体)必须执行以下操作: https://www.googleapis.com/mirror/v1/(请注意,没有上传字词。)

相关问题