如何在Android super.loadURL方法中检测HTTP 404?

时间:2014-06-05 07:13:19

标签: android cordova

我在Android环境中使用PhoneGap,我想知道在执行super.loadURL(myUrl)后如何检测/捕获HTTP 404?

1 个答案:

答案 0 :(得分:0)

您需要@Override onReceivedError方法。

public class HelloWorld extends CordovaActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.init();
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        // super.loadUrl("file:///android_asset/www/index.html");

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.apache.cordova.CordovaActivity#onReceivedError(int,
     * java.lang.String, java.lang.String)
     */
    @Override
    public void onReceivedError(int errorCode, String description,
            String failingUrl) {
        // TODO Auto-generated method stub
        // super.onReceivedError(errorCode, description, failingUrl);
        if (errorCode == 404) {
            // show Alert here for Page Not found
            Log.i("log", "Page not found 404");
        } else {

            Log.i("log", "Success");
        }

    }
}