下面是我的代码:
public void mywebview(){
try {
webview.setBackgroundColor(0);
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new MyJavaScriptInterface(), "MYOBJECT");
webview.getSettings().setAllowUniversalAccessFromFileURLs(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setAllowFileAccess(true);
webview.canScrollHorizontally(1);
webview.setInitialScale(1);
webview.getSettings().setDefaultFontSize(2);
webview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
webview.setHorizontalScrollBarEnabled(true);
webview.setInitialScale(1);
return false;
}
});
webview.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(view.getContext(), "Authentication Error", Toast.LENGTH_LONG).show();
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setCookie(USERNAME, PASSWORD);
super.onReceivedError(view, errorCode, description, failingUrl);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
}
public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
SharedPreferencesManager mgr = SharedPreferencesManager.getInstance();
StringBuilder stringBuilder = new StringBuilder();
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
}
} catch (Exception e) {
}
CookieStore cookieStore = mgr.getCookieStore();
Cookie cookie = cookieStore.getCookies().get(1);
CookieManager cookieMgr = CookieManager.getInstance();
cookieMgr.setCookie(url, cookie.getName() + "=" + cookie.getValue());
CookieSyncManager.getInstance().sync();
return super.shouldInterceptRequest(view, url);
}
});
webCookieManager = CookieManager.getInstance();
webCookieManager.setAcceptCookie(true);
CookieStore cookieStore = mgr.getCookieStore();
Cookie cookie = cookieStore.getCookies().get(1);
webCookieManager.setCookie(URL, cookie.getName() + "=" + cookie.getValue() + ";");
CookieSyncManager.getInstance().sync();
webview.loadUrl(URL);
} catch (Exception e) {
e.printStackTrace();
}
}
看起来webresourceresponse只兼容棒棒糖版本及以上版本。有没有一种方法可以修复它,使其适用于较低版本,即果冻豆和kitkat?此外,我所说的"混乱"是指它看起来都垂直对齐,塞进一个视图中,不像棒棒糖+版本,视图看起来像预期的那样组织得很好。这是我的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
>
<WebView
android:id="@+id/my_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
我认为没有办法改变这一点,除非你使用旧的方法来处理旧的API参见此
最好/唯一的方法是编写特定于版本的代码,一个用于jelly-bean和kitkat,另一个用于lollypop及以上版本。我还没有研究过让它在较低版本上工作的代码,但处理它的方法是检查SDK版本,并执行特定版本的代码。
示例:(代码修改自:如何在您的代码中支持多个android版本?)
// System information
private final int sdkVersion = Build.VERSION.SDK_INT;
// If you want to go really old:
// (actually, there is a question about how this issue should be handled
// systematically. Suggestions welcome.)
// final int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
// For meaning of other variable, see notification documentation on the android website.
if (sdkVersion > Build.VERSION_CODES.LOLLIPOP) {
myWebviewForLollipopAndAbove webview = new myWebviewForLollipopAndAbove();
//Do things with the webview, finish implementing
}
else {
myWebviewForBelowLollipop webview = new myWebviewForBelowLollipop();
//Do things with the webview, finish implementing
}
显然,这段代码必须放在初始化web视图的地方,并且您需要为其他SDK版本创建另一个web视图类。