开发者

How to avoid redirection of 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.loadU开发者_JAVA技巧rl("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.


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

How to avoid redirection of Url?


Change the user agent string using WebView.setUserAgentString(String ua) to a desktop browser. That way, facebook will not know user is on a mobile phone.

More info

List of all user agent strings: http://www.useragentstring.com/pages/useragentstring.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜