我想從當前完成的AsyncTask
的onPostExecute
調用新的AsyncTask
。從onPostExecute方法調用另一個AsyncTask
protected void onPostExecute(Integer feed) {
dialog1.dismiss();
super.onPostExecute(feed);
new SendsequenceofRequest().execute();
}
但我收到以下異常:
android.os.NetworkOnMainThreadException
我logcat的輸出是:
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()
方法啓動另一個線程。
class SendsequenceofRequest extends AsyncTask<String, Void, Integer> {
// private ProgressDialog dialog1;
protected void onPreExecute() {
// dialog1 = ProgressDialog.show(MainScreen.this, "", "Loading...");
}
protected void onPostExecute(Integer feed) {
// dialog1.dismiss();
super.onPostExecute(feed);
}
@Override
protected Integer doInBackground(String... arg0) {
String str_stater_menu = "Starters";
String response_starter = "";
try {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (isThreadPaused1) {
try {
Thread.sleep(3000);
mHandler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
final String response_req_sequence = SimpleHttpClient.sendresponseSequReqRes(response_send_order);
System.out.println(" i ma in thread");
if(response_req_sequence != null)
{
onPause();
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Your Order will be delivered in"+response_req_sequence, 10000).show();
}
});
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (Exception e) {
// TODO: handle exception
}
}
}
}).start();
//response_starter = SimpleHttpClient.sendFirstStarter(str_stater_menu);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("i am getting response for starter"+response_starter);
// str_reponse_request_tableid_ip = SimpleHttpClient.sendIpTabletId(url , str_ip);
/**
*
*/
// res = response_starter.toString();
System.out.println("response TEST: "+res);
// Object obj = response_starter.toString();
System.out.println("after object creation ");
// getJsonStringForStarter(response_starter);
System.out.println("response :"+res);
/** Inside the new thread we cannot update the main thread
So updating the main thread outside the new thread */
try {
}catch (Exception e) {
e.printStackTrace();
// e.printStackTrace();
}
return null;
}
}
當我通過幾個鏈接,我找不到任何幫助作爲我的問題。所以請不要讓身體幫我把我帶出這個問題。在此先感謝所有人。
爲什麼在AsyncTask中創建線程?他們的重點是,所以你不必管理你的線程。 – Geobits
感謝您的回覆。我不得不連續發送請求到服務器,直到我沒有得到任何迴應。此請求將在第一次請求完成後執行。那爲什麼我在onPostExecute方法中使用它。如果您有其他解決方案或建議,請幫助我.. – DJhon
您可以在'doInBackground'中執行此操作。只需在Thread.sleep(1000)中添加一個'while(true)'循環,一旦成功完成一個'break' – Tseng