0

Goo.gl APIを使用する場合、末尾にスラッシュを自動的に追加しないように指示する方法はありますか?たとえば、http://www.samsung.com/us/support/SupportOwnersFAQPopup.do? faq_id = FAQ00046726&fm_seq = 49755にアクセスすると、多くのWebサイトが混乱するため、スラッシュが付いていても機能しません。助言がありますか?

私のコード:

  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 サービスを使用して URL を短縮するための優れたインターフェイスを提供する、私が作成したライブラリの使用を検討してください。

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 に答える