【问题标题】:My webview messes up in jelly bean and kitkat version ,but works fine in lollipop and above. How do I fix it for jelly bean and kitkat?我的 webview 在 jelly bean 和 kitkat 版本中搞砸了,但在棒棒糖及更高版本中运行良好。我该如何解决果冻豆和奇巧?
【发布时间】:2016-03-28 13:06:52
【问题描述】:

这是我的代码:

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 仅与棒棒糖版本及更高版本兼容。有没有办法修复它以适用于较低版本的果冻豆和奇巧?此外,通过“混乱”,我的意思是它看起来所有垂直对齐的东西都塞进一个视图中,不像棒棒糖+版本,视图看起来像预期的那样井井有条。这是我的 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>

【问题讨论】:

    标签: android android-webview android-relativelayout android-webservice android-websettings


    【解决方案1】:

    我认为没有办法可以改变这一点,除非你对旧 API see this 使用旧方法@

    【讨论】:

    • 我该怎么做?这个例子太模糊了
    【解决方案2】:

    最好/唯一的方法是编写特定于版本的代码,一种用于果冻豆和奇巧,另一种用于棒棒糖及更高版本。我还没有研究过可以使其在较低版本上工作的代码,但接近它的方法是检查 SDK 版本,并执行特定版本的代码。

    示例:(代码修改自:How to support multiple android version in your code?

    // 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 版本创建另一个 webview 类。

    【讨论】:

    • 但这不是我面临的问题。问题是是否可以以某种方式使 public WebResourceResponse shouldInterceptRequest(WebView view, String url) 向后兼容?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    相关资源
    最近更新 更多