Android WebView Not Loading Mobile Site

2020-03-07 05:13发布

My app shows a list of articles from an RSS feed and when one is selected it shows the article in a web view. The problem is that the web view is displaying the desktop site instead of the mobile version and I can't seem to figure out why.

WebView Code:

package com.kentuckyfarmbureau.kyfb;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.RenderPriority;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;

public class WebBrowser extends Activity {
    WebView web;
    ProgressBar prgPageLoading;
    ImageButton btnBack, btnForward;
    Button btnShare;
    String myURL;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.webbrowser);

        Intent iGet = getIntent();
        myURL = iGet.getStringExtra("myURL");

        web = (WebView) findViewById(R.id.web);
        prgPageLoading = (ProgressBar) findViewById(R.id.prgPageLoading);
        btnBack = (ImageButton) findViewById(R.id.btnBack);
        btnForward = (ImageButton) findViewById(R.id.btnForward);
        btnShare = (Button) findViewById(R.id.btnShare);

        web.getSettings().setDomStorageEnabled(true);
        web.getSettings().setJavaScriptEnabled(true);
        web.getSettings().setSupportZoom(true);
        web.getSettings().setBuiltInZoomControls(false);
        web.getSettings().setUserAgentString("Android");

        web.getSettings().setRenderPriority(RenderPriority.HIGH);
        web.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

        web.loadUrl(myURL);

        btnShare.setOnClickListener(new View.OnClickListener() {

            //@Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub


                String webUrl = web.getUrl();
                String webTitle = web.getTitle();

                final String p0 = "KYFB Share:";
                final String p1 = "Kentucky Farm Bureau";
                final String p2 = "'Big On Commitment.'";



                Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.setType("text/html");
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, webTitle);
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, p0 + "\n\n" + webUrl + "\n\n\n" +
                   p1 + "\n" + p2 +  "\n\n");

                startActivity(Intent.createChooser(emailIntent, "Send your email in:"));

            }
        });

        btnBack.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(web.canGoBack()){
                    web.goBack();
                } else 
                    finish();
            }
        });

        btnForward.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(web.canGoForward()){
                    web.goForward();
                }
            }
        });

        final Activity act = this;
        web.setWebChromeClient(new WebChromeClient(){
            public void onProgressChanged(WebView webview, int progress){

                act.setProgress(progress*100);
                prgPageLoading.setProgress(progress);

            }


        });

        web.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted( WebView view, String url, Bitmap favicon ) {

                super.onPageStarted( web, url, favicon );
                prgPageLoading.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished( WebView view, String url ) {

                super.onPageFinished( web, url );
                prgPageLoading.setProgress(0);
                prgPageLoading.setVisibility(View.GONE);

            }   
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                 Toast.makeText(act, description, Toast.LENGTH_SHORT).show();
               }

            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {

                if(url.endsWith(".mp4") || url.endsWith(".3gp") || url.endsWith(".avi")){
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i); //warning no error handling will cause force close if no media player on phone.
                    return true;
                }

                view.loadUrl(url);
                return true;
            }
            });



    }



}

5条回答
老娘就宠你
2楼-- · 2020-03-07 05:51

As @WarrenFaith pointed out in comments, this might be happening because of the custom User-Agent string that you are setting with

web.getSettings().setUserAgentString("Android");

Do not set this or make sure your webserver is configured to serve the mobile version of the site when it receives request with user agent as "Android".

查看更多
在下西门庆
3楼-- · 2020-03-07 05:52

You just need to do include following line of code and it will be loading mobile site perfectly!

webView.setInitialScale(1);
webView.getSettings().setJavaScriptEnabled(true);
查看更多
ら.Afraid
4楼-- · 2020-03-07 05:55

Setting setUseWideViewPort to false solved the problem for me:

mWebView.getSettings().setUseWideViewPort(false);
查看更多
一夜七次
5楼-- · 2020-03-07 05:57
  WebSettings settings = view.getSettings();
      settings.setJavaScriptCanOpenWindowsAutomatically(true);
      settings.setSupportMultipleWindows(true);
      settings.setBuiltInZoomControls(true);
      settings.setJavaScriptEnabled(true);
      settings.setAppCacheEnabled(true);
      settings.setAppCacheMaxSize(10 * 1024 * 1024);
      settings.setAppCachePath("");
      settings.setDatabaseEnabled(true);
      settings.setDomStorageEnabled(true);
      settings.setGeolocationEnabled(true);
      settings.setSaveFormData(false);
      settings.setSavePassword(false);
      settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
      // Flash settings
      settings.setPluginState(WebSettings.PluginState.ON);

      // Geo location settings
      settings.setGeolocationEnabled(true);
      settings.setGeolocationDatabasePath("/data/data/selendroid");
    } catch (Exception e) {
      SelendroidLogger.error("Error configuring web view", e);
    }
查看更多
看我几分像从前
6楼-- · 2020-03-07 06:05

After searching a lot this worked for me -

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);

    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setDisplayZoomControls(false);

    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    webView.setScrollbarFadingEnabled(false);
查看更多
登录 后发表回答