3

Web ビューで「Web ページが利用できません」というエラー ページの代わりに警告ボックスを表示しようとしていますが、ドキュメントに従って onReceivedError() メソッドを追加した後でも機能しません。コード...

public class CloudPageHolder extends Activity {
WebView mWebView;
ProgressDialog _dialog ;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mWebView = (WebView) findViewById(R.id.webview);


    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.loadUrl("file:///android_asset/www/MyPage.html");
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
    mWebView.getSettings().setPluginsEnabled(false);
    mWebView.getSettings().setSupportMultipleWindows(false);
    mWebView.getSettings().setSavePassword(false);
    mWebView.getSettings().getAllowFileAccess();
    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.getSettings().setLoadWithOverviewMode(true);

    mWebView.setWebViewClient(new WebViewClient() {  
        public void onReceivedError(WebView view, int errorCode, String description,     String failingUrl) {  
          new AlertDialog.Builder(CloudPageHolder.this)  
                .setMessage("Something went wrong!")  
                .setPositiveButton(android.R.string.ok,  
                        new AlertDialog.OnClickListener()  
                        {  
                           public void onClick(DialogInterface dialog, int which)  
                            { 

                               dialog.dismiss();
                            }  
                        })  
                .setCancelable(false)  
                .create()  
                .show();  
                }  
        });  


    mWebView.setWebChromeClient(new WebChromeClient() {  
        @Override  
        public boolean onJsAlert(WebView view, String url, final String message, final android.webkit.JsResult result)  
        {  
            new AlertDialog.Builder(CloudPageHolder.this)  
                .setMessage(message)  
                .setPositiveButton(android.R.string.ok,  
                        new AlertDialog.OnClickListener()  
                        {  
                           public void onClick(DialogInterface dialog, int which)  
                            { 
                               result.confirm();
                            }  
                        })  
                .setCancelable(false)  
                .create()  
                .show();  

            return true;  
        } 
  });
 }

前もって感謝します...

4

2 に答える 2

3

あなたのエラーが何であるかはわかりませんが、 onReceivedError のドキュメントはやや誤解を招く可能性があります。この時点で、このメソッドを使用して HTTP エラー応答をインターセプトすることはできません。これらの投稿を確認してください。

この他の投稿で説明されている方法を使用して、同様の回避策を開発しています。

近日中に詳細を更新します。幸運を!

于 2012-11-02T14:35:29.927 に答える
1

最終的にこれを行う方法を見つけましたが、きれいではありません。XMLHttpRequest を介して JavaScript でページを読み込むことができます。これにより、ステータス コードにアクセスできます。これにより、進行状況の通知が台無しになり、他の望ましくない動作も発生する可能性があります。しかし、それは私のアプリでは受け入れられたので、他の人の助けになるかもしれません.

public class StatusNotifyingWebView extends WebView {

    public interface OnStatusReceivedListener {
        void onStatusReceived(int statusCode);
    }

    private class JsInterface {
        public void notifyRequestStatus(final int statusCode) {
            getHandler().post(new Runnable() {
                @Override
                public void run() {
                    mOnStatusReceivedListener.onStatusReceived(statusCode);
                }
            });
        }
    }

    private OnStatusReceivedListener mOnStatusReceivedListener;

    public void setOnStatusReceivedListener(final OnStatusReceivedListener listener) {
        mOnStatusReceivedListener = listener;
        addJavascriptInterface(new JsInterface(), "statusNotifyJsInterface");
    }

    public void loadUrlWithStatusEvent(final String url) {
        Assert.assertNotNull(mOnStatusReceivedListener);
        final String script =
                "  var httpRequest = new XMLHttpRequest();\n"
                + "httpRequest.open('GET', '" + url + "', false);\n"
                + "try { httpRequest.send(null); } catch (err) { }\n"
                + "statusNotifyJsInterface.notifyRequestStatus(httpRequest.status);\n"
                + "document.write(httpRequest.responseText);\n"
                + "document.close();\n";

        final String html = "<html><head><script>" + script + "</script></head><body/></html>";
        loadDataWithBaseURL(url, html, "text/html", "UTF-8", null);
    }
}
于 2013-04-29T00:07:29.050 に答える