Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting blank page in Android WebViewClient

I am trying open twitter link : http://mobile.twitter.com/pawan_rathore88 in my activity. If I set WebViewClient to webview I am getting blank page. But when I load url without setting any webviewclient, it loads page properly. Does anyone have idea what can be a problem. Following is my code snippet.

webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
//if I comment the following line then webpage loads properly in default Android browser.
webview.setWebViewClient(new WebViewClient() {
   public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                 Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
               }
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

    Log.v(tag, "url :" + url);
    view.loadUrl(url);
    return true;
        }
 });
    webview.loadUrl("http://mobile.twitter.com/pawan_rathore88");

Thanks, Pawan

like image 567
Pawan Avatar asked Nov 14 '22 11:11

Pawan


1 Answers

After tweaking the code around, it seems to be a user agent problem, seems that changing it to a desktop useragent fixes this problem :

  WebView web = (WebView)findViewById(R.id.webView1);
        web.getSettings().setUserAgentString("Mozilla/5.0 (Macintosh; " +
            "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " +
            "like Gecko) Version/5.0 Safari/533.16");

        String url = "http://mobile.twitter.com/pawan_rathore88";
        web.loadUrl(url);
like image 64
JoxTraex Avatar answered Nov 16 '22 04:11

JoxTraex