禁用wifi时AsyncTask出错

时间:2011-06-22 16:57:41

标签: android rss android-asynctask

我在我的应用程序中解析rss feed我在加载rss时尝试使用进度条。这是我第一次使用AsyncTask。当我按下rss的按钮时,对话框出现并且当rss停止时load.So,它的工作..但是,我从我的logcat检查,当wifi关闭时,我得到以下错误:

06-22 19:43:16.744: ERROR/OSFP.News(2316): java.lang.RuntimeException: java.net.ConnectException: feeds.feedburner.com/209.85.146.118:80 - Network is unreachable
06-22 19:43:16.744: ERROR/OSFP.News(2316): java.lang.RuntimeException: java.lang.RuntimeException: java.net.ConnectException: feeds.feedburner.com/209.85.146.118:80 - Network is unreachable
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at com.example.async.DomFeedParser.parse(DomFeedParser.java:54)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at com.example.async.nea.loadFeed(nea.java:62)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at com.example.async.nea.onCreate(nea.java:51)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at android.os.Looper.loop(Looper.java:123)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at android.app.ActivityThread.main(ActivityThread.java:3687)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at java.lang.reflect.Method.invokeNative(Native Method)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at java.lang.reflect.Method.invoke(Method.java:507)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at dalvik.system.NativeStart.main(Native Method)
06-22 19:43:16.744: ERROR/OSFP.News(2316): Caused by: java.lang.RuntimeException: java.net.ConnectException: feeds.feedburner.com/209.85.146.118:80 - Network is unreachable
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at com.example.async.BaseFeedParser.getInputStream(BaseFeedParser.java:32)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at com.example.async.DomFeedParser.parse(DomFeedParser.java:26)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     ... 15 more
06-22 19:43:16.744: ERROR/OSFP.News(2316): Caused by: java.net.ConnectException: feeds.feedburner.com/209.85.146.118:80 - Network is unreachable
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:207)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at java.net.Socket.connect(Socket.java:1002)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1018)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:512)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     at com.example.async.BaseFeedParser.getInputStream(BaseFeedParser.java:30)
06-22 19:43:16.744: ERROR/OSFP.News(2316):     ... 16 more

在我的新闻活动中,我已将其用于关闭wifi:

catch (Throwable t){
            Log.e("OSFP.News",t.getMessage(),t);

            Toast.makeText(nea.this, "Please enable your wifi",
                    Toast.LENGTH_SHORT).show();
            finish();
        }

另外,当我按下按钮开始在我的主要活动中加载新闻时,我会使用asyncTask:

nea = (Button) findViewById(R.id.nea);
        nea.setOnClickListener(new Button.OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    new BackgroundAsyncTask().execute();
                    nea.setClickable(false);
                }});


//.......
public class BackgroundAsyncTask extends
       AsyncTask<Void, Integer, Void> {
          private ProgressDialog dialog;
        int myProgress;

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            nea.setClickable(true);
             dialog.dismiss();

        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
              dialog = ProgressDialog.show(main.this, "", "Loading. Please wait...", true);
            myProgress = 0;
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            Intent newActivity111 = new Intent(main.this, nea.class);     
            startActivity(newActivity111);
            return null;
        }



    }

另外,在我的DomFeedParser课程中,我有一个例外:

catch (Exception e) {
            throw new RuntimeException(e);
        } 

1 个答案:

答案 0 :(得分:1)

不要捕获Exception类

catch (Exception e) {
    throw new RuntimeException(e);
}

相反,您应该优雅地处理连接错误

点击此处查看如何正确执行:http://source.android.com/source/code-style.html