1

我开发了一个小型 android webview 应用程序来访问基于 PHP 的内部(本地网络)站点。

这是我的代码:

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);
    }

}

该站点具有身份验证,但我想在应用程序和站点之间添加一些身份验证(我该怎么做?可能在调用 url 时传递一个参数),其次,更重要的是,我具体将 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中看到了这个解释, 但我不知道在哪里实现它。

先感谢您

4

2 回答 2

2

您需要创建扩展 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());

希望这可以帮助

于 2012-12-19T02:04:30.107 回答
1

您可以添加onReceivedError您的HelloWebViewClient课程并在遇到错误时处理您想要处理的内容。

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();
        }
    }
于 2017-08-25T12:46:57.187 回答