gpt4 book ai didi

android - 生成 httprequest 以从 android 镜像 api 服务的正确方法

转载 作者:太空狗 更新时间:2023-10-29 13:26:36 25 4
gpt4 key购买 nike

我尝试从我的 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

有什么想法吗?

附言。还有另一种方法可以将时间线卡从安卓应用程序推送到谷歌眼镜吗?

谢谢

最佳答案

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

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

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

--编辑--您想要执行的请求(只有元数据,没有多媒体)必须完成: https://www.googleapis.com/mirror/v1/ (注意这里没有上传字样)。

关于android - 生成 httprequest 以从 android 镜像 api 服务的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20704770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com