gpt4 book ai didi

android - 在 android 中使用 Google Url Shortener Api 时出现解析错误

转载 作者:太空狗 更新时间:2023-10-29 15:10:47 37 4
gpt4 key购买 nike

我正在尝试在我的 Android 应用程序中创建对 Google Shortener Api 的 POST 请求。但它返回代码为 400 的解析错误消息。这是我的代码...我是否遗漏了任何内容?

final String GOOGL_URL = "https://www.googleapis.com/urlshortener/v1/url";
String tinyUrl = null;
final Gson gson = new Gson();

try
{
InputStream in;
HttpClient client = new DefaultHttpClient();
HttpResponse response;
JSONObject json = new JSONObject();
HttpPost post = new HttpPost(GOOGL_URL);
json.put("key", "my_key");
json.put("longUrl", longUrl);
post.setHeader("json", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
post.setEntity(se);

response = client.execute(post);
in = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuilder resultString = new StringBuilder();
String lineGet;

while ((lineGet = br.readLine()) != null) {
resultString.append(lineGet);
}
if (Common.DEBUG) {
Log.d(TAG, "Request string : " + json.toString());
Log.d(TAG, "Response string : " + resultString);
}

GooGlResult result = gson.fromJson(resultString.toString(),
GooGlResult.class);

return result.getId();
}
catch (Exception ex) {
System.out.println("UrlShortener "+ex);
return longUrl;
}

提前致谢..

最佳答案

1)

final String GOOGL_URL = "https://www.googleapis.com/urlshortener/v1/url?key={API_KEY}";

2)

json.put("longUrl", longUrl);
post.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
StringEntity se = new StringEntity(json.toString());
post.setEntity(se);

关于android - 在 android 中使用 Google Url Shortener Api 时出现解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16726125/

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