2012-07-16 149 views
0

使用Goo.gl API時,是否有辦法告訴它不要自動添加尾部斜槓?因爲它弄亂了很多網站,例如,如果你去:http://www.samsung.com/us/support/SupportOwnersFAQPopup.do?faq_id=FAQ00046726&fm_seq=49755與一個尾隨斜線它不起作用!有什麼建議麼?Goo.gl API自動添加尾部斜槓

我的代碼:

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(); 
      } 
+1

你將有github repo here向我們展示一些代碼,以便我們可以看到你錯在哪裏。 – 2012-07-16 19:16:14

+0

將該URL投入到goo.gl中會使我http://goo.gl/nugyd不會追加尾部斜線。你在問什麼? – 2012-07-16 19:17:16

+0

@DanielDiPaolo我的意思是它給Long Url增加了一個斜槓 – 2012-07-16 19:18:23

回答

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

您要添加的longurl自己後斜線,goo.gl沒有這樣做。

+1

我不敢相信我是多麼的愚蠢......謝謝:) – 2012-07-16 19:22:15

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! 
} 

看看其中包含進一步的相關信息:)