我有一个远程网址,我想在我的网页视图中加载它。此页面由标题保护。页面正在加载,但它没有加载我试图加载的HTML内部的相对URL。
相同的网址完全适用于网络,但未在此处加载。主要问题是它没有加载java脚本文件。
这是我的代码:
WebSettings webViewSettings = webView.getSettings();
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setDomStorageEnabled(true);
webViewSettings.setSupportZoom(true);
webViewSettings.setBuiltInZoomControls(true);
webViewSettings.setDisplayZoomControls(true);
webViewSettings.setAllowFileAccess(true);
webViewSettings.setLoadWithOverviewMode(true);
webViewSettings.setUseWideViewPort(true);
webViewSettings.setPluginState(WebSettings.PluginState.ON);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(Report.this,"Finished loading",Toast.LENGTH_SHORT).show();
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
try {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(flip_book);
httpGet.setHeader("Cookie", "JSESSIONID=" + preferencesHelper.getPref("JSESSIONID") + "; X-CSRF-TOKEN=" + preferencesHelper.getPref(preferencesHelper.csrftoken));
httpGet.setHeader("JSESSIONID", preferencesHelper.getPref("JSESSIONID"));
httpGet.setHeader("X-CSRF-TOKEN", URLEncoder.encode(preferencesHelper.getPref(preferencesHelper.csrftoken), "UTF-8"));
HttpResponse httpReponse = client.execute(httpGet);
InputStream responseInputStream = httpReponse.getEntity().getContent();
return new WebResourceResponse("text/html", "charset=UTF-8", responseInputStream);
//text/html
} catch (ClientProtocolException e) {
//return null to tell WebView we failed to fetch it WebView should try again.
return null;
} catch (IOException e) {
//return null to tell WebView we failed to fetch it WebView should try again.
return null;
}
}
});
webView.loadData("<iframe src=\"" +
"" +
flip_book +
"\"></iframe>", "text/html",
"utf-8");
请帮忙。
答案 0 :(得分:0)
请尝试使用以下代码,可能应该有效:
WebSettings webViewSettings = webView.getSettings();
webViewSettings.setJavaScriptEnabled(true);
webViewSettings.setDomStorageEnabled(true);
webViewSettings.setSupportZoom(true);
webViewSettings.setBuiltInZoomControls(true);
webViewSettings.setDisplayZoomControls(true);
webViewSettings.setAllowFileAccess(true);
webViewSettings.setLoadWithOverviewMode(true);
webViewSettings.setUseWideViewPort(true);
webViewSettings.setPluginState(WebSettings.PluginState.ON);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(Report.this,"Finished loading",Toast.LENGTH_SHORT).show();
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
try {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(flip_book);
httpGet.setHeader("Cookie", "JSESSIONID=" + preferencesHelper.getPref("JSESSIONID") + "; X-CSRF-TOKEN=" + preferencesHelper.getPref(preferencesHelper.csrftoken));
httpGet.setHeader("JSESSIONID", preferencesHelper.getPref("JSESSIONID"));
httpGet.setHeader("X-CSRF-TOKEN", URLEncoder.encode(preferencesHelper.getPref(preferencesHelper.csrftoken), "UTF-8"));
HttpResponse httpReponse = client.execute(httpGet);
InputStream responseInputStream = httpReponse.getEntity().getContent();
return new WebResourceResponse("text/html", "charset=UTF-8", responseInputStream);
//text/html
} catch (ClientProtocolException e) {
//return null to tell WebView we failed to fetch it WebView should try again.
return null;
} catch (IOException e) {
//return null to tell WebView we failed to fetch it WebView should try again.
return null;
}
}
});
webView.loadDataWithBaseURL("<iframe src=\"" +
"" +
flip_book +
"\"></iframe>", "text/html",
"utf-8",null);
答案 1 :(得分:0)
你可以尝试使用OkHTTP这样的东西:
loader.showLoader();
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
loader.dismissLoader();
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
loader.dismissLoader();
showAlert("Something went wrong","Ok");
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
try {
String url = request.getUrl().toString();
return getNewResponse(url);
} catch (Exception e) {
//return null to tell WebView we failed to fetch it WebView should try again.
return null;
}
}
private WebResourceResponse getNewResponse(String url) {
try {
OkHttpClient httpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(url.trim())
.addHeader("Cookie", "JSESSIONID=" + preferencesHelper.getPref("JSESSIONID") + "; X-CSRF-TOKEN=" + preferencesHelper.getPref(preferencesHelper.csrftoken))
.addHeader("JSESSIONID", preferencesHelper.getPref("JSESSIONID"))
.addHeader("X-CSRF-TOKEN", URLEncoder.encode(preferencesHelper.getPref(preferencesHelper.csrftoken), "UTF-8")).build();
Response response = httpClient.newCall(request).execute();
return new WebResourceResponse(
null,
response.header("content-encoding", "utf-8"),
response.body().byteStream()
);
} catch (Exception e) {
return null;
}
}
});
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl(flip_book);