2015-11-03 61 views
0

我有一个缩短的url完成http://goo.gl/ 我需要得到原始的网址。在ANDROID中有没有这样做?谷歌URL缩短解码android

我试做短什么 -

编译 '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! 
} 
+0

为了什么,我得到了反对票的人吗? – Anirban

+0

如果你不能帮助,请不要给予否定的投票。我没有相关的解决方案,我搜索了近2个小时,这就是为什么我问。 – Anirban

回答

1

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

我打开一个网页视图不可见性,然后调用url.Then在页面加载完成我一个抓取网址时

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

无法测试现场的权利,但这个应该是工作。

+0

你能给出完整的解决方案吗? – Anirban