1

这是我的代码:

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>
4

2 回答 2

0

我不认为有一种方法可以改变它,除非你对旧 API 使用旧方法,请参阅此

于 2016-04-04T20:33:26.387 回答
-1

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

于 2016-04-05T17:29:31.893 回答