0

使用 Goo.gl API 时,有没有办法告诉它不要自动添加斜杠?因为它弄乱了很多网站,例如,如果您访问: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();
            }
4

2 回答 2

4
post.setEntity(new StringEntity("{\"longUrl\": \"" +longurl+ "/\"}"));
// I think I found the problem -------------------------------^ 

您在longurl自己之后添加斜线,goo.gl 没有这样做。

于 2012-07-16T19:21:44.473 回答
0

还可以考虑使用我制作的库,它提供了一个很好的界面来使用 Goo.gl 服务缩短您的网址。

它支持 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 存储库,其中包含更多信息:)

于 2014-10-30T22:15:57.627 回答