Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid redirection of Url?

Tags:

android

url

i have to simply open facebook on my mobile phone, facebook's browser version. but in my mobile i can only open its mobile version. How can i avoid this redirection of Url.

m using this code now.

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class WebViewExample extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.facebook.com");
        webView.setWebViewClient(new HelloWebViewClient());
}
}

Its HelloWebViewClient class is as follows.

import android.webkit.WebView;
import android.webkit.WebViewClient;

public class HelloWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onLoadResource(WebView view, String url)
    {
        if (url.equals("http://www.facebook.com"))
        {
            //do your own thing here
            view.scrollTo(300, 0);
        }
        else
        {
            super.onLoadResource(view, url);
        }           
    }

M using Android 2.3.4, My device is google Nexus one. Kindly suggest me where i am going wrong and what sud i do to avoid this type of URL Redirection.

like image 385
Gaurav Gupta Avatar asked Feb 24 '23 08:02

Gaurav Gupta


1 Answers

Set the User-Agent header before the loadUrl()

String yourUserAgent
   = "User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
yourWebView.getSettings().setUserAgentString(yourUserAgent);

I will check what is the bedst User-Agent to use but I think this will work perfectly

Tested and working

Tested and working

like image 143
Sherif elKhatib Avatar answered Mar 07 '23 23:03

Sherif elKhatib