首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Goo.gl API自动添加尾部斜杠

Goo.gl API自动添加尾部斜杠
EN

Stack Overflow用户
提问于 2012-07-17 03:14:45
回答 2查看 678关注 0票数 0

在使用Goo.gl应用程序接口时,有没有办法告诉它不要自动添加尾随斜杠?因为它弄乱了很多网站,例如,如果你用一个尾随斜杠来访问:http://www.samsung.com/us/support/SupportOwnersFAQPopup.do?faq_id=FAQ00046726&fm_seq=49755,它就不能工作!有什么建议吗?

我的代码:

代码语言:javascript
复制
  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();
            }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-17 03:21:44

代码语言:javascript
复制
post.setEntity(new StringEntity("{\"longUrl\": \"" +longurl+ "/\"}"));
// I think I found the problem -------------------------------^ 

您自己在longurl后面添加了斜杠,goo.gl没有这么做。

票数 4
EN

Stack Overflow用户

发布于 2014-10-31 06:15:58

还可以考虑使用我制作的库,它提供了一个很好的接口,可以使用Goo.gl服务缩短您的urls。

它支持api密钥,使用起来非常简单:

代码语言:javascript
复制
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 :)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11510988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档