Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

java - Force mobile version of website for WebView

I'm loading an URL into a webview to display it into my app. The problem I'm encountering is, that not always the site recognizes that I'm a phone (why so ever?). How exactly do I force the webview to send to the site that I'm a mobile phone? Currently I'm doing it like that

webview.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3");

But that doesn't work? Won't it work because I'm not using an iPhone? I don't think that this is the reason since it's just setting the user Agent...

This is the relevant code (the irelevant code just contains data such as getting an url from an intent and formatting a string )

//package

//imports

public class WebViewing extends Activity {

    private WebView webview;
    private ProgressDialog dialog;

    //init strings

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webviewer);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        //init variables

        //get intent data and format string


        this.webview = (WebView) findViewById(R.id.webView1);
        webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        webview.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3"); 
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
            }

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        dialog.setMessage("Website wird geladen...
Dies ist abhu00E4ngig von deiner Internet-Verbindung.");
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        webview.loadUrl(sourceURL);
        setTitle("Platz: " + plusRank + " - " + realDate);

    }

    //onCreateOptionsMenu method

    //onOptionsItemSelected method
}

I also tried out this string for the user agent

Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

try to put these following lines :-

webview.getSettings().setJavaScriptEnabled(true);
 webview.getSettings().setUserAgentString("Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3"); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...