谷歌URL缩短解码android

时间:2015-11-03 14:28:17

标签: android google-url-shortener

http://goo.gl/我缩短了网址 我需要获得原始网址。在ANDROID中是否有任何api。

我试图缩短的时间 -

  

编译' com.andreabaccega:googlshortenerlib:1.0.0'

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!
} else if ( Status.IO_EXCEPTION.equals(result.getStatus()) ) {
    // connectivity error. result.getException() returns the thrown exception while performing
    // the request to google servers!
} else {
    // Status.RESPONSE_ERROR
    // this happens if google replies with an unexpected response or if there are some other issues processing
    // the result.

    // result.getException() contains a GooglShortenerException containing a message that can help resolve the issue!
}

2 个答案:

答案 0 :(得分:2)

使用HttpURLConnection加载ShortURL,然后您可以使用

读出目标网址
httpURLConnection.getHeaderField("location");

完整解决方案

URL url = new URL("http://goo.gl/6s8SSy");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
Log.v("Full URL", httpURLConnection.getHeaderField("location"));

现在无法测试,但这应该有效。

答案 1 :(得分:1)

我做了我的解决方案。 我做了什么 -

我打开一个没有可见性的webview然后调用那个url.Then在页面加载完成后我获取了url

WebView webView;
webView = (WebView)findViewById(R.id.help_webview);
webview.loadUrl("http://goo.gl/tDn72f");
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
    String myResult = webView.getUrl();
}
});