A J
A J

Reputation: 4652

Google URL shorten decode android

I have a shorten url done by http://goo.gl/ I need to get the original url. Is there any api to do that in ANDROID.

What I tried for make shorter -

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

Upvotes: 0

Views: 2221

Answers (2)

LilaQ
LilaQ

Reputation: 350

Load the ShortURL with a HttpURLConnection, then you can read out the target URL with

httpURLConnection.getHeaderField("location");

Full solution

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

Can't test live right now, but this should be working.

Upvotes: 2

A J
A J

Reputation: 4652

I made my solution. What I did -

I open a webview without visibility then call that url.Then on page load complete I a fetching the 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();
}
});

Upvotes: 1

Related Questions