2013-04-29 28 views
0

我通過其中包含的AsyncTask外部類的手段retreive從服務器數據:進度中的AsyncTask - onPostExecute方法後,顯示

public class GetTask extends AsyncTask<String, Void, JSONObject> { 

    private Context context; 
    ProgressDialog dialog; 

    public GetTask(Context cxt) { 
     context = cxt; 
     dialog = new ProgressDialog(context); 
    } 

    protected void onPreExecute() { 
     dialog.setTitle("Load..."); 
     dialog.setMessage("Data..."); 
     dialog.show(); 

     super.onPreExecute(); 
    } 

    @Override 
    protected JSONObject doInBackground(String... url) { 

     // code for retreive data 
     return jArray; 

    } 

    protected void onPostExecute(JSONObject object) { 
     dialog.dismiss(); 
     super.onPostExecute(object); 
    } 
} 

我把這個任務從我的活動:檢索

Tasks task = new Tasks(); 
JSONObject json = task.new GetTask(this).execute(ServerURL).get(); 

我的數據成功但ProgressDialog顯示後super.onPostExecute(對象);方法,爲什麼?

P.S.對話框顯示後:

// Make sure the identity of this thread is that of the local process, 
    // and keep track of what that identity token actually is. 
    Binder.clearCallingIdentity(); 
    final long ident = Binder.clearCallingIdentity(); 

內部Looper.class

對不起我的英語不好。 ))

+0

你是否在你的任務? – Blackbelt 2013-04-29 11:59:26

+0

你的意思是 - dialog.setCancelable(true); ? – xck 2013-04-29 12:02:22

+0

不,我的意思是task.cancel(true)。你確定onPostExecute被調用嗎? – Blackbelt 2013-04-29 12:04:02

回答

0

我找到了解決方案,需要使用回調,而不是使用.get()方法。我打電話給我的任務:

callTask(linkODeatails, obj); 

callTask​​:

void callTask(String link, String object){ 
    task.new GetTask(this).execute(link + object); 

} 

創建界面:

public interface AsyncTaskCompleteListener { 
public void onTaskComplete(JSONObject result); 

}

而且在我的任務中添加:

 private Activity activity; 
    private AsyncTaskCompleteListener callback; 

    public GetTask(Activity act){ 
     this.activity = act; 
     this.callback = (AsyncTaskCompleteListener)act; 
    } 

他們叫:

callback.onTaskComplete(result, object); 
相關問題