从发布的Android到的goo.gl [英] Posting to goo.gl from Android
本文介绍了从发布的Android到的goo.gl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请让这样的:
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): }
使用此code:
using this code:
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";
}
我不知道如何格式化这一点。任何想法?
I'm not sure how to format this. Any ideas?
我试着删除 UrlEn codedFormEntity
,但当然,这是行不通的。
I tried removing UrlEncodedFormEntity
, but of course that wouldn't work.
推荐答案
您需要将数据发送为JSON,无法形成连接codeD,你正在尝试做的。
You need to send the data as json, not form encoded as you are trying to do.
看看文档这里
更改该实体是这样一个StringEntity:
Change the entity to be a StringEntity like this:
post.setEntity(new StringEntity("{\"longUrl\": \"http://www.google.com/\"}"));
另外设置请求的内容类型:
Also set the content type of the request:
post.setHeader("Content-Type", "application/json");
这篇关于从发布的Android到的goo.gl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文