我试图在我的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);

10-08 18:50