1

這裏是我的代碼爲:我的webview混淆了果凍豆和kitkat版本,但在棒棒糖及以上版本中工作正常。我該如何修復它的果凍豆和kitkat?

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> 

回答

0

我不認爲這是你可以改變,除非你使用的老辦法老API see this

+0

我該怎麼做?那個例子太模糊 –

0

最好的/只有這樣,才能做到這一點會的方式是編寫特定版本的代碼,一個用於果凍豆和kitkat,另一個用於棒棒糖和以上。我還沒有研究過能夠在較低版本上工作的代碼,但接近它的方法是檢查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版本。

+0

但那不是我面臨的問題。問題是如果公共WebResourceResponse shouldInterceptRequest(WebView視圖,String url)可以以某種方式向後兼容? –

相關問題