onPostExecute没有被ASyncTask execute()调用

时间:2013-04-15 13:36:33

标签: android android-asynctask

我无法解释为什么我的代码中没有调用onPostExecute。我以前使用不同的应用程序成功使用了几乎这个确切的代码。它打印'onPreExecute'和在return result;之前从doInBackground获取JSON的成功结果,但是onPostExecute不打印任何东西 - 超级调用与否 - 而且不会将我的字符串返回给UI线程。有什么想法吗?

public class PrivateLoadFromServer extends AsyncTask<String, Integer, String> {

    InputStream is = null;
    String result = null;
    String error_text="";
    JSONObject j = null;
    String url;
    SQLiteOpenHelper helper;
    CustomActivity activity;
    private Context context;
    private ProgressDialog dialog;
    private int count;
    String employee;
    String text;

    public PrivateLoadFromServer(CustomActivity activity,String employee,String url){
        this.url=url;
        this.employee=employee;
        this.activity=activity;
    }

    @Override
    protected void onPreExecute() {
        system.out.println("onPreExecute");

    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        System.out.println("onPostExecute");

        activity.setJSONResult(result);
        /*
        if(result!=null){
            System.out.println("Task reported successful");
            taskHandler.taskSuccessful(this.result);
            activity.setJSONResult(this.result);
        } else {
            taskHandler.taskFailed();
        }
        */
        //return;
    }

    @Override
    protected String doInBackground(String... params) {
        System.out.println("Running aSyncTask");

            // Successful code to get JSON result string from server omitted.

        System.out.println("Raw at doInBackground: " + result);
    return result;
编辑:本来应该发布这个,我把它称为CustomActivity中的内部类,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_login);
    helper=new DBHelper(this);
    loadBasicData();

}

 private void loadBasicData() {

    // If you can encode all of these into one JSON Object, cool.

    String url="*****" //Omitted URL.
    String employee=null;

    PrivateLoadFromServer syncTask = new PrivateLoadFromServer(this,employee,url);

    syncTask.execute();

    try {
        syncTask.get(15000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TimeoutException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println("JSONResult at Activity:"+jsonResult); // jsonResult is null

    }
}

    public void setJSONResult(String result){
    System.out.println("setJSONResult"+result);
    this.jsonResult=result; // jsonResult is a field in the Activity.
}

3 个答案:

答案 0 :(得分:0)

检查你是否在postExecute之前明确取消代码中的AsyncTask。

答案 1 :(得分:0)

因为你正在调用get,所以JSON结果也会从这个方法返回(参见AsyncTask.java的源代码。所以你可以直接在输出上调用你的方法。

try {
    setJSONResult(syncTask.get(15000, TimeUnit.MILLISECONDS));
} catch (InterruptedException e) {
    e.printStackTrace();
} catch (ExecutionException e) {
    e.printStackTrace();
} catch (TimeoutException e) {
    e.printStackTrace();
}

答案 2 :(得分:0)

在主UI线程上调用onPostExecute()。如果没有被调用,则意味着您的UI线程被阻止执行其他操作。