简体   繁体   中英

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.

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 is http://rise.esprit-apps.com/faqs/mobilefaq

That means it's not working in Kitkat, Lollipop, or Marshmallow.

I think it's a cookies problem, but I have enabled cookies in WebView but can't resolve this.

The same URL is working in Chrome and another browser perfectly but it's not loading in default 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:

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 (API level 19) introduces a new version of WebView that is based on Chromium. This change upgrades WebView performance and standards support for HTML5, CSS3, and JavaScript to match the latest web browsers. Any apps using WebView will inherit these upgrades when running on Android 4.4 and higher.

Reference Link: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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