从onPostExecute方法调用另一个AsyncTask

时间:2013-09-30 15:50:26

标签: android android-asynctask networkonmainthread

我想从当前已完成的AsyncTask的{​​{1}}中拨打新的onPostExecute

AsyncTask

但我收到以下异常:

          protected void onPostExecute(Integer feed) {
        dialog1.dismiss();          
        super.onPostExecute(feed);
        new SendsequenceofRequest().execute();
    } 

我的logcat输出是:

        android.os.NetworkOnMainThreadException

在我的09-30 16:08:19.517: W/System.err(3706): android.os.NetworkOnMainThreadException 09-30 16:08:19.517: W/System.err(3706): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1077) 09-30 16:08:19.517: W/System.err(3706): at dalvik.system.BlockGuard$WrappedNetworkSystem.read(BlockGuard.java:279) 09-30 16:08:19.517: W/System.err(3706): at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:448) 09-30 16:08:19.517: W/System.err(3706): at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:75) 09-30 16:08:19.517: W/System.err(3706): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103) 09-30 16:08:19.517: W/System.err(3706): at org.apache.http.impl.io.SocketInputBuffer.isStale(SocketInputBuffer.java:132) 09-30 16:08:19.517: W/System.err(3706): at org.apache.http.impl.AbstractHttpClientConnection.isStale(AbstractHttpClientConnection.java:205) 09-30 16:08:19.517: W/System.err(3706): at org.apache.http.impl.conn.AbstractClientConnAdapter.isStale(AbstractClientConnAdapter.java:185) 09-30 16:08:19.577: W/System.err(3706): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:336) 09-30 16:08:19.577: W/System.err(3706): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 09-30 16:08:19.577: W/System.err(3706): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 09-30 16:08:19.597: W/System.err(3706): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 09-30 16:08:19.597: W/System.err(3706): at com.dm.ekot.reqres.SimpleHttpClient.sendresponseSequReqRes(SimpleHttpClient.java:715) 09-30 16:08:19.607: W/System.err(3706): at com.dm.ekot.MainScreen$SendsequenceofRequest$1$1.run(MainScreen.java:1020) 09-30 16:08:19.620: W/System.err(3706): at android.os.Handler.handleCallback(Handler.java:587) 09-30 16:08:19.620: W/System.err(3706): at android.os.Handler.dispatchMessage(Handler.java:92) 09-30 16:08:19.620: W/System.err(3706): at android.os.Looper.loop(Looper.java:132) 09-30 16:08:19.620: W/System.err(3706): at android.app.ActivityThread.main(ActivityThread.java:4025) 09-30 16:08:19.627: W/System.err(3706): at java.lang.reflect.Method.invokeNative(Native Method) 09-30 16:08:19.627: W/System.err(3706): at java.lang.reflect.Method.invoke(Method.java:491) 09-30 16:08:19.627: W/System.err(3706): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 09-30 16:08:19.627: W/System.err(3706): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 09-30 16:08:19.642: W/System.err(3706): at dalvik.system.NativeStart.main(Native Method) SendsequenceofRequest中,我确实在AsyncTask方法中启动了另一个帖子。

doInBackground()

当我通过几个链接时,我找不到任何帮助作为我的问题。所以,请不要身体帮助我带我离开这个问题。在此先感谢所有人。

3 个答案:

答案 0 :(得分:2)

我建议使用publishProgress发布当前进度并覆盖在主线程上执行的onProgressUpdate。这样,您就不必在AsyncTask中使用处理程序和线程。

请注意,第一行已从extends AsyncTask<String, Void, Integer>更改为extends AsyncTask<String, String, Integer>。 middle参数确定更新参数的对象类型。如果需要将复杂数据发送到update方法,请将其更改为包含所有必要信息的类。

class SendsequenceofRequest extends AsyncTask<String, String, Integer> {
    @Override
    protected Integer doInBackground(String... arg0) {    
        try {
            // This will run indefinitely, unless stopped by break. So make sure you have an exit condition, see below
            String response_req_sequence = null;
            do {
                response_req_sequence = SimpleHttpClient.sendresponseSequReqRes(response_send_order);
                if(response_req_sequence != null) {
                    // Instead of calling runOnUiThread, call progress update
                    publishProgress(response_req_sequence);
                }

                // Wait before retry
                Thread.sleep(3000);

            // Keep in mind, that if the connection is successful, the string may be empty,
            // so TextUtils.isEmpty(response_req_sequence); may be better exit condition as it checks if string is null or has size of 0
            } while(response_req_sequence!=null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    void onProgressUpdate(String... string) {
        // This will always be executed in main thread
        onPause();
        Toast.makeText(getApplicationContext(), "Your Order will be delivered in"+response_req_sequence, 10000).show();
    }

    protected void onPostExecute(Integer feed) {
        // Notify user the task has bene completed
        Toast.makeText(getApplicationContext(), "Task completed.", 10000).show();
    }        
}

答案 1 :(得分:0)

如果从主/ UI线程创建mHandler,那么发布到它的任何Runnable也将在主线程上运行。由于您尝试在Runnable内执行网络I / O,因此该应用正在抛出NetworkOnMainThreadException

答案 2 :(得分:0)

Runnable由

开始
mHandler.post(Runnable runnable);

在主线程上执行,您尝试在可运行的内部使用网络。

要避免此问题,只需创建新线程并按以下方式启动:

Thread t = new Thread(new Runnable {
...
});
t.start();