2012-12-19 53 views
0

我開發了一個小的android webview應用程序來訪問一個內部(本地網絡)基於PHP的網站。Android的webview頁面不可用

這裏是我的代碼:

package com.CheckInventory; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.WindowManager; 
import android.webkit.WebChromeClient; 
import android.webkit.WebSettings; 
import android.webkit.WebStorage; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

@SuppressWarnings("unused") 
public class CheckInventoryActivity extends Activity { 
    WebView webview; 
    String username; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
     setContentView(R.layout.main); 
     webview = (WebView) findViewById(R.id.webview); 
     WebView webView = (WebView) findViewById(R.id.webview); 
     webView.setBackgroundColor(0); 
     webView.setBackgroundResource(R.drawable.myimage); 
     webView.addJavascriptInterface(new JavaScriptInterface(this), "Android"); 
     WebSettings webSettings = webview.getSettings(); 
     webSettings.setLoadWithOverviewMode(true); 

     webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 

     webSettings.setJavaScriptEnabled(true); 
     webSettings.setJavaScriptCanOpenWindowsAutomatically(true); 
     webSettings.setDatabasePath("/data/data/"+this.getPackageName()+"/databases/"); 
     webSettings.setDomStorageEnabled(true); 
     webview.setWebChromeClient(new WebChromeClient()); 
     webview.loadUrl("http://192.168.0.124/android"); 
     webview.setWebViewClient(new HelloWebViewClient()); 

    } 
    private class HelloWebViewClient extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 
    } 


    @Override 
    public boolean onKeyDown (int keyCode, KeyEvent event) { 
     if((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()){ 
      //webview.goBack(); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

} 

該網站有認證,但我想添加的應用和網站之間(一些認證如何做到這一點傳遞參數也許當網址是什麼?被調用),其次,更重要的是,我特別在哪裏放置了onReceivedError,以便用戶如果離開建築物或鬆散連接,就永遠不會看到url或頁面。

public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) { 
     Toast.makeText(Webform.this, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show(); 
    } 

我在Detecting Webview Error and Show Message看到了這個解釋,但我不知道在哪裏實現它。

預先感謝您

回答

1

您需要創建延伸WebViewClient和實現方法OnReceivedError

這樣

class myWebClient extends WebViewClient { 

    @Override 
    public void onReceivedError(WebView view, int errorCode, 
      String description, String failingUrl) { 
     Toast.makeText(Webform.this, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show(); 
     super.onReceivedError(view, errorCode, description, failingUrl); 
    } 

} 

,然後你需要新的WebViewClient設爲您的WebView類

web = (WebView) findViewById(R.id.webview); 
    web.setWebViewClient(new myWebClient()); 

希望這會有所幫助

+0

UnQuaiz:我不知道如何實現你的答案,你能告訴我整個代碼,你會代碼嗎? – MrM

+0

[基於您的代碼示例](http://pastebin.com/e6SEJ1xa) – UnQuaiz

+0

我真誠地感謝您的時間!不幸的是,它沒有奏效。它仍然顯示未找到和網址。最終,我正在尋找類似於谷歌播放的解決方案,如果互聯網不可用,它會顯示一個重試按鈕。由於它是由谷歌的傢伙完成的,所以我可能很奇怪這是多麼困難。 – MrM

0

您可以在您的HelloWebViewClient類中添加onReceivedError,並處理髮生錯誤時要處理的內容。

private class HelloWebViewClient extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 

     @Override 
     public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){ 
      //Your code to do 
     Toast.makeText(getActivity(), "Your Internet Connection May not be active Or " + error , Toast.LENGTH_LONG).show(); 
     } 
    }