WebView
有一个问题,一个链接只是在WebView
加载,但它无效。
相同的URL
适用于Android操作系统版本4.1,4.2,4.2和4.3,但它在版本4.4及更高版本中无效。
Url
http://rise.esprit-apps.com/faqs/mobilefaq
这意味着它无法在Kitkat,Lollipop或Marshmallow工作。
我认为这是一个Cookie问题,但我已在WebView
启用了Cookie,但无法解决此问题。
相同的URL
在Chrome和其他浏览器中运行良好,但未在默认WebView
中加载。
如果有人有解决方案,请告诉我。
这是我的代码:
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();
}
}
我在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>
谢谢。
答案 0 :(得分:0)
请检查在Android 4.4中迁移到WebView
Android 4.4(API级别19)引入了基于Chromium的新版WebView。此更改升级了对HTML5,CSS3和JavaScript的WebView性能和标准支持,以匹配最新的Web浏览器。在Android 4.4及更高版本上运行时,任何使用WebView的应用程序都将继承这些升级。
参考链接:http://developer.android.com/guide/webapps/migrating.html
希望它会对你有所帮助。
答案 1 :(得分:0)
试试这段代码:
CookieManager cm = CookieManager.getInstance();
cm.setAcceptCookie(true);
应该在创建WebView之前执行。