我们正在使用标题加载WebView
(在这种情况下必须加载)。没有它就不会打开。我们尝试了将标头发送到WebView
的所有方法,但这似乎只能起作用。现在的问题是,用标题打开网页内容需要2-3分钟,但是当在服务器级别删除证券时,它会像魅力一样打开。请建议一种快速打开WebView
的方法。
我尝试启用和禁用硬件加速,但它没有帮助。
这是我们的Java代码:
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import com.appiness.beroe.R;
import com.appiness.beroe.utils.FontsHelper;
import com.appiness.beroe.utils.PreferencesHelper;
import java.net.URLEncoder;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class Report extends BaseActivity {
WebView webView;
PreferencesHelper preferencesHelper;
ImageView ivBack;
TextView tvTitle;
String flip_book;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report);
preferencesHelper = new PreferencesHelper(Report.this);
flip_book = getIntent().getStringExtra("FLIP_BOOK");
initializeViews();
stylizeViews();
tvTitle.setText("");
// For API level below 18 (This method was deprecated in API level 18)
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
if (Build.VERSION.SDK_INT >= 19) {
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else {
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
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().setDomStorageEnabled(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.loadUrl(flip_book);
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
private String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
switch (extension) {
case "js":
return "text/javascript";
case "woff":
return "application/font-woff";
case "woff2":
return "application/font-woff2";
case "ttf":
return "application/x-font-ttf";
case "eot":
return "application/vnd.ms-fontobject";
case "svg":
return "image/svg+xml";
}
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
private void initializeViews()
{
webView = (WebView) findViewById(R.id.wvReports);
ivBack = (ImageView) findViewById(R.id.ivBack);
tvTitle = (TextView) findViewById(R.id.tvTitle);
}
private void stylizeViews()
{
tvTitle.setTypeface(FontsHelper.Titillium_Regular(Report.this));
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
}