从 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): }
使用这个代码:
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
,但这当然行不通。
Keep getting this:
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): }
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?
I tried removing UrlEncodedFormEntity
, but of course that wouldn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要以 json 形式发送数据,而不是像您尝试那样以表单编码的形式发送数据。
请查看此处的文档。
将实体更改为 StringEntity,如下所示:
还设置请求的内容类型:
You need to send the data as json, not form encoded as you are trying to do.
Take a look at the documentation here.
Change the entity to be a StringEntity like this:
Also set the content type of the request:
还可以考虑使用我制作的一个库,它提供了一个很好的界面,可以使用 Goo.gl 服务来缩短您的网址。
它支持 api 密钥并且非常易于使用:
查看 github 存储库< /a> 其中包含更多信息:)
Also consider using a library I made that provide a nice interface to shorten your urls using Goo.gl service.
It supports the api key and is very easy to use:
Take a look at the github repo here which contains further infos :)