0

我有一个由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!
}
4

2 回答 2

2

用 加载 ShortURL HttpURLConnection,然后你可以用 读出目标 URL

httpURLConnection.getHeaderField("location");

完整的解决方案

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

现在无法进行现场测试,但这应该可以工作。

于 2015-11-03T15:06:51.263 回答
1

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

我打开一个不可见的 webview 然后调用该 url。然后在页面加载完成时我获取 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();
}
});
于 2015-11-03T15:49:08.310 回答