gpt4 book ai didi

android - 从 Android 发布到 goo.gl

转载 作者:太空宇宙 更新时间:2023-11-03 13:05:06 25 4
gpt4 key购买 nike

继续获取:

V/RESPONSE(13605):  {
V/RESPONSE(13605): "error": {
V/RESPONSE(13605): "errors": [
V/RESPONSE(13605): {
V/RESPONSE(13605): "domain": "global",
V/RESPONSE(13605): "reason": "parseError",
V/RESPONSE(13605): "message": "This API does not support parsing form-encoded input."
V/RESPONSE(13605): }
V/RESPONSE(13605): ],
V/RESPONSE(13605): "code": 400,
V/RESPONSE(13605): "message": "This API does not support parsing form-encoded input."
V/RESPONSE(13605): }
V/RESPONSE(13605): }

使用此代码:

String apiKey = "blahblahblah";
String address="https://www.googleapis.com/urlshortener/v1/url";
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(address);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("key", apiKey));
pairs.add(new BasicNameValuePair("longUrl", original));

try {
post.setEntity(new UrlEncodedFormEntity(pairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
org.apache.http.HttpResponse response = client.execute(post);
String responseBody = EntityUtils.toString(response.getEntity());
Log.v("RESPONSE"," "+responseBody);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tinyUrl="Protocol Error";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
tinyUrl="IO Error";
}

我不确定如何格式化它。有什么想法吗?

我尝试删除 UrlEncodedFormEntity,但这当然行不通。

最佳答案

您需要将数据作为 json 发送,而不是像您尝试的那样以编码形式发送。

查看文档 here .

像这样将实体更改为 StringEntity:

post.setEntity(new StringEntity("{\"longUrl\": \"http://www.google.com/\"}"));

同时设置请求的内容类型:

post.setHeader("Content-Type", "application/json");

关于android - 从 Android 发布到 goo.gl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6052156/

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