Android 我的webview在jelly bean和kitkat版本中出错,但在棒棒糖和更高版本中工作良好。我如何为果冻豆和kitkat制作它?
以下是我的代码:Android 我的webview在jelly bean和kitkat版本中出错,但在棒棒糖和更高版本中工作良好。我如何为果冻豆和kitkat制作它?,android,android-webview,android-relativelayout,android-webservice,android-websettings,Android,Android Webview,Android Relativelayout,Android Webservice,Android Websettings,以下是我的代码: public void mywebview(){ try { webview.setBackgroundColor(0); webview.getSettings().setJavaScriptEnabled(true); webview.addJavascriptInterface(new MyJavaScriptInterface(), "MYOBJECT"); webview.getSettings().setAllowUniversalA
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版本,并为特定版本执行代码 示例:代码修改自:
显然,这段代码必须放在初始化web视图的位置,您需要为其他SDK版本创建另一个webview类。我该怎么做?这个例子太模糊了,但这不是我面临的问题。问题是,如果PublicWebResourceResponse应该InterceptRequestWebView视图,字符串url可以以某种方式向后兼容吗?
// 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
}