我试图在我的webview上加载一个web url,它是一个payment URL(页面显示卡详细信息输入屏幕)。这个url在浏览器上工作,它在浏览器上加载位它在android webview上不工作。它记录onPageStarted()
和onPageFinished()
,但它的很多显示任何错误,如onReceivedError()
,onReceivedSslError()
我试过这个Some url not loading in webview buts loading in the browser,但它也不适合我,
这是我的代码
private void loadPaymentURL(){
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setAllowFileAccessFromFileURLs(true);
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
webView.clearCache(true);
webView.clearHistory();
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptEnabled(true); // enable javascript
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setDisplayZoomControls(false);
webView.getSettings().setAllowFileAccess(false);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setDatabaseEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
Log.e("WEBVIEW", "PaymentURL : "+paymentUrl);
try {
paymentUrl = getFinalURL(paymentUrl);
}catch (Exception e){}
Log.e("WEBVIEW", "FinalURL : "+paymentUrl);
webView.loadUrl(paymentUrl);
}
getFinalURL()
方法我从这个链接Some url not loading in webview buts loading in the browser得到这个,但它也不适合我,
public static String getFinalURL(String url) throws IOException {
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setInstanceFollowRedirects(false);
con.connect();
con.getInputStream();
if (con.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM || con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
String redirectUrl = con.getHeaderField("Location");
return getFinalURL(redirectUrl);
}
return url;
}
这是我的webview
的WebViewClient
,
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.getSettings().setJavaScriptEnabled(true);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.e("WEBVIEW", "onPageStarted() : "+url);
progressLayout.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.e("WEBVIEW", "onPageFinished() : "+url);
progressLayout.setVisibility(View.GONE);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
Log.e("WEBVIEW", "onReceivedError1() : "+error);
view.goForward();
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e("WEBVIEW", "onReceivedError2() URL: "+failingUrl+" , Description:"+description);
view.goForward();
progressLayout.setVisibility(View.GONE);
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
Log.e("WEBVIEW", "onReceivedSslError() : "+error.toString());
//progressLayout.setVisibility(View.GONE);
view.goForward();
}
}
1条答案
按热度按时间cunj1qz11#
我尽了最大努力来找出问题,但不幸的是无法得到的网址未加载的原因。
但是如果你很急,你可以用这个Github库,因为它工作得很好。
https://github.com/delight-im/Android-AdvancedWebView
电码