我正在尝试在网络视图中加载网页,但是我遇到以下异常:
java.io.FileNotFoundException: https://fonts.googleapis.com/css?family=Roboto:400,300,500,700,900,700italic
02-05 18:52:44.497 5263-6265/com.RM W/System.err: at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
02-05 18:52:44.497 5263-6265/com.RM W/System.err: at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
02-05 18:52:44.497 5263-6265/com.RM W/System.err: at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
02-05 18:52:44.497 5263-6265/com.RM W/System.err: at navigationfragments.FragmentWebView$1.shouldInterceptRequest(FragmentWebView.java:124)
02-05 18:52:44.497 5263-6265/com.RM W/System.err: at android.webkit.WebViewClient.shouldInterceptRequest(WebViewClient.java:125)
02-05 18:52:44.498 5263-6265/com.RM W/System.err: at com.android.webview.chromium.WebViewContentsClientAdapter.shouldInterceptRequest(WebViewContentsClientAdapter.java:297)
02-05 18:52:44.498 5263-6265/com.RM W/System.err: at org.chromium.android_webview.AwContents$BackgroundThreadClientImpl.shouldInterceptRequest(AwContents.java:465)
02-05 18:52:44.498 5263-6265/com.CRM W/System.err: at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(AwContentsBackgroundThreadClient.java:38)
但令我惊讶的是,如果我点击链接:
https://fonts.googleapis.com/css?family=Roboto:400,300,500,700,900,700italic
我实际上可以在浏览器中加载它。许多opther文件也会发生这种情况,这些文件通常可以从浏览器访问。我在这里错过了什么?
以下是我的自定义实现:
WebViewClient webViewClient = new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
startAnim();
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (webView.getProgress() == 100) {
stopAnim();
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
NoNetworkDialog.showNetworkDialog(getActivity());
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!CheckConnection.isNetworkConnected(getActivity())) {
NoNetworkDialog.showNetworkDialog(getActivity());
return false;
} else {
view.loadUrl(url);
return true;
}
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url1) {
// if (!CheckConnection.isNetworkConnected(getActivity())) {
// NoNetworkDialog.showNetworkDialog(getActivity());
// } else {
try {
URL url = new URL(url1);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
CookieManager cookieManager = CookieManager.getInstance();
String cookie = cookieManager.getCookie(url.getHost());
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Cookie", cookie);
urlConnection.setRequestProperty("***_USER_AGENT", "***_app");
urlConnection.setRequestMethod("GET");
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
return new WebResourceResponse(urlConnection.getContentType(), "UTF-8", in);
} catch (Exception e) {
e.printStackTrace();
return null;
}
// }
// return null;
}
};
这是我得到这个的另一个网址:
https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js
https://www.google.com/jsapi
https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js
删除setDoOutput()
,看看它是否有效。您没有执行 POST 请求,也不需要它。
参考链接在这里。