在使用Goo.gl应用程序接口时,有没有办法告诉它不要自动添加尾随斜杠?因为它弄乱了很多网站,例如,如果你用一个尾随斜杠来访问:http://www.samsung.com/us/support/SupportOwnersFAQPopup.do?faq_id=FAQ00046726&fm_seq=49755,它就不能工作!有什么建议吗?
我的代码:
address= "https://www.googleapis.com/urlshortener/v1/url?key=xxxxxxxxxxxxx"
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(address);
try {
post.setEntity(new StringEntity("{\"longUrl\": \"" +longurl+ "/\"}"));
post.setHeader("Content-Type", "application/json");
if (userLogin == true) {
post.setHeader("Authorization", "OAuth "+accessToken);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
org.apache.http.HttpResponse response = client.execute(post);
String responseBody = EntityUtils.toString(response.getEntity());
JSONObject object = (JSONObject) new JSONTokener(responseBody).nextValue();
query = object.getString("id");
shortUrl = query;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}发布于 2012-07-17 03:21:44
post.setEntity(new StringEntity("{\"longUrl\": \"" +longurl+ "/\"}"));
// I think I found the problem -------------------------------^ 您自己在longurl后面添加了斜杠,goo.gl没有这么做。
发布于 2014-10-31 06:15:58
还可以考虑使用我制作的库,它提供了一个很好的接口,可以使用Goo.gl服务缩短您的urls。
它支持api密钥,使用起来非常简单:
GoogleShortenerPerformer shortener = new GoogleShortenerPerformer(new OkHttpClient());
String longUrl = "http://www.andreabaccega.com/";
GooglShortenerResult result = shortener.shortenUrl(
new GooglShortenerRequestBuilder()
.buildRequest(longUrl)
);
if ( Status.SUCCESS.equals(result.getStatus()) ) {
// all ok result.getShortenedUrl() contains the shortened url!
}看一下包含更多信息的github repo here :)
https://stackoverflow.com/questions/11510988
复制相似问题