简体   繁体   English

WebView无法在Android 4.4+中加载url

[英]WebView not loading url in Android 4.4+

I have one issue with WebView , one link that simply loads in WebView but it's not working. 我有一个关于WebView问题,一个链接只是在WebView加载,但是它不起作用。

The same URL is working fine with versions 4.1, 4.2,4.2.2, and 4.3 of Android OS, but it's not working in versions 4.4 and above. 相同的URL可以在4.1、4.2、4.2.2和4.3的Android OS上正常运行,但在4.4和更高版本中不起作用。

Url is http://rise.esprit-apps.com/faqs/mobilefaq Urlhttp://rise.esprit-apps.com/faqs/mobilefaq

That means it's not working in Kitkat, Lollipop, or Marshmallow. 这意味着它不能在Kitkat,Lollipop或棉花糖中使用。

I think it's a cookies problem, but I have enabled cookies in WebView but can't resolve this. 我认为这是一个cookie问题,但是我已经在WebView启用了cookie,但无法解决此问题。

The same URL is working in Chrome and another browser perfectly but it's not loading in default WebView . 相同的URL在Chrome和其他浏览器中都可以正常运行,但是不会在默认的WebView加载。

If anyone has a solution, please let me know. 如果有人有解决方案,请告诉我。

Here is my code: 这是我的代码:

package com.avm.ui;

import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.avm.uc.CustAnimatedActivity;
import com.avm.uc.CustomProgressBarDialog;
import com.avm.uc.Header;
import com.avm.util.Config;
import com.avm.util.Pref;
import com.avm.util.Utils;
import com.esp.therisemethod.R;

public class EvaluateYourSelfEsteemActivity extends CustAnimatedActivity {

    /* Declaration of variable here */

    private Header header;
    private WebView webView;
    private CustomProgressBarDialog mProgressDialog;
    private int flag;
    private DefaultHttpClient httpClient;
    public static Cookie cookie = null;

    // Flag 1=for EvaluateYourSelfEsteem
    // Flag 2=for Faq

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_common_webview);
        flag = getIntent().getIntExtra("FLAG", 0);
        Utils.addActivities(EvaluateYourSelfEsteemActivity.this);

        /* fetch object resource here */

        header = (Header) findViewById(R.id.header);
        /* setValue in Header */
        if (flag == 1) {
            header.txtTitle.setText(getResources().getString(
                    R.string.RATE_YOUR_SELF_ESTEEM));
        } else if (flag == 2) {
            header.txtTitle.setText(getResources().getString(R.string.FAQs));
        } else if (flag == 3) {
            header.txtTitle.setText(getResources().getString(
                    R.string.PREMIUM_ACCOUNT));
        }
        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new WebViewClient());
        webView.clearHistory();
        webView.clearView();

        WebSettings settings = webView.getSettings();
        settings.setDefaultTextEncodingName("utf-8");
        settings.setJavaScriptEnabled(true);
        settings.setAllowFileAccess(true);
        settings.setLoadWithOverviewMode(true);
        settings.setPluginState(PluginState.ON);
        settings.setRenderPriority(RenderPriority.HIGH);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setDomStorageEnabled(true);
        settings.setAllowUniversalAccessFromFileURLs(true);

        settings.setDisplayZoomControls(false);
        settings.setUseWideViewPort(false);
        if (Build.VERSION.SDK_INT >= 21) {
            CookieSyncManager.createInstance(this);
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.removeAllCookie();
            cookieManager.setAcceptThirdPartyCookies(webView, true);
            cookieManager.setAcceptCookie(true);
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_NEVER_ALLOW);
        }
        webView.setBackgroundColor(Color.TRANSPARENT);
        if (Utils.isTablet(EvaluateYourSelfEsteemActivity.this))
            settings.setTextSize(WebSettings.TextSize.LARGER);
        else
            settings.setTextSize(WebSettings.TextSize.NORMAL);
        /** set background layer */
        if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
        }

        if (flag == 1) {
            webView.loadUrl("http://www.riseselfesteem.com/test/");
        } else if (flag == 2) {
            webView.loadUrl("http://192.168.1.42/work/rise/faqs/mobilefaq");
            // webView.loadUrl("http://rise.esprit-apps.com/faqs/mobilefaq");

        } else if (flag == 3) {
            webView.loadUrl("http://192.168.1.42/work/rise/payments/mobilepay/"
                    + Pref.getValue(EvaluateYourSelfEsteemActivity.this,
                            Config.PREF_USERID, "0"));
        }

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (mProgressDialog == null) {
                    mProgressDialog = new CustomProgressBarDialog(
                            EvaluateYourSelfEsteemActivity.this);
                    mProgressDialog.setCancelable(true);
                    mProgressDialog.show();
                }
            }

            // load URL
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.equals("http://www.riseselfesteem.com/video")) {
                    view.stopLoading();
                    startAnimatedActivity(new Intent(
                            EvaluateYourSelfEsteemActivity.this,
                            AboutUsActivity.class),
                            CustAnimatedActivity.SLIDE_FROM_RIGHT);
                    return false;
                } else if (url
                        .equals("http://192.168.1.42/work/rise/contact?mobilecontact")) {
                    view.stopLoading();
                    startAnimatedActivity(new Intent(
                            EvaluateYourSelfEsteemActivity.this,
                            ContectUsActivity.class),
                            CustAnimatedActivity.SLIDE_FROM_RIGHT);
                    return false;
                } else if (url.equals("http://paymentfail/")) {
                    view.stopLoading();
                    finish();
                    overridePendingTransition(
                            R.anim.animated_activity_slide_left_in,
                            R.anim.animated_activity_slide_right_out);
                    return false;
                } else if (url.equals("http://paymentdone/")) {
                    view.stopLoading();
                    Pref.setValue(EvaluateYourSelfEsteemActivity.this,
                            Config.PREF_IS_PAID, "1");
                    startAnimatedActivity(new Intent(
                            EvaluateYourSelfEsteemActivity.this,
                            PremiumAccountSucessActivity.class)
                            .addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT),
                            CustAnimatedActivity.SLIDE_FROM_RIGHT);
                    finish();
                    return false;
                } else {
                    view.loadUrl(url);
                    return true;
                }

            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                if (mProgressDialog != null && mProgressDialog.isShowing()) {
                    mProgressDialog.cancel();
                    mProgressDialog = null;
                }

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (mProgressDialog != null && mProgressDialog.isShowing()) {
                    mProgressDialog.cancel();
                    mProgressDialog = null;

                }
            }
        });

        super.onCreate(savedInstanceState);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Save the state of the WebView
        webView.saveState(outState);
    }

    // onRestoreInstanceState to restore the instance
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Restore the state of the WebView
        webView.restoreState(savedInstanceState);
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onDestroy() {
        try {
            webView.clearView();

        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        if (mProgressDialog != null && mProgressDialog.isShowing())
            mProgressDialog.dismiss();
        mProgressDialog = null;
        super.onPause();
    }

    @Override
    public void onBackPressed() {
        if (mProgressDialog != null && mProgressDialog.isShowing())
            mProgressDialog.dismiss();
        mProgressDialog = null;
        super.onBackPressed();

    }
}

I got below Error in logcat: 我在logcat中得到以下错误:

12-31 10:29:11.223: I/art(11886): Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.FloatingSelectActionModeCallback>
12-31 10:29:11.228: I/art(11886): Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.FloatingSelectActionModeCallback>
12-31 10:29:11.246: I/art(11886): Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.WebViewContentsClientAdapter$WebResourceErrorImpl>
12-31 10:29:11.247: I/art(11886): Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.WebViewContentsClientAdapter$WebResourceErrorImpl>

Thank you. 谢谢。

Please Check Migrating to WebView in Android 4.4 请检查在Android 4.4中迁移到WebView

Android 4.4 (API level 19) introduces a new version of WebView that is based on Chromium. Android 4.4(API级别19)引入了基于Chromium的WebView的新版本。 This change upgrades WebView performance and standards support for HTML5, CSS3, and JavaScript to match the latest web browsers. 此更改将升级WebView性能,并支持HTML5,CSS3和JavaScript的标准以匹配最新的Web浏览器。 Any apps using WebView will inherit these upgrades when running on Android 4.4 and higher. 在Android 4.4和更高版本上运行时,所有使用WebView的应用程序都将继承这些升级。

Reference Link: http://developer.android.com/guide/webapps/migrating.html 参考链接: http : //developer.android.com/guide/webapps/migrating.html

Hope it will help you. 希望对您有帮助。

Try this code: 试试这个代码:

CookieManager cm = CookieManager.getInstance();
cm.setAcceptCookie(true);

It should be executed before WebView is created. 它应该在创建WebView之前执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM