Android 如何将数据从asynctask类返回到oncreate方法

Android 如何将数据从asynctask类返回到oncreate方法,android,Android,我想访问一些值(test1、test2、test3、topic1),这些值正在成功地从数据库中检索,并显示在Postexecute中的LOGCAT中。 但是当我尝试在PostAsync之外调用它们时,它会返回NULL。 如何在PostAsync之外获得这些变量的实际值 modify_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v)

我想访问一些值(test1、test2、test3、topic1),这些值正在成功地从数据库中检索,并显示在Postexecute中的LOGCAT中。 但是当我尝试在PostAsync之外调用它们时,它会返回NULL。 如何在PostAsync之外获得这些变量的实际值

modify_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

              String evtdt = eventdate.getText().toString();
                      String evtloc = item;
                      String orgnm = orgname2.getText().toString();

                      new PostAsync().execute(evtdt,evtloc,orgnm);

                Log.d("test1outside",test1);//Showing NULL
                Log.d("test2outside",test2);//Showing NULL
                Log.d("test3outside",test3);//Showing NULL
                Log.d("Topic1outside",Topic1);//Showing NULL

            }
        }
    });
}


class PostAsync extends AsyncTask<String, String, JSONObject> {

    JSONParser jsonParser = new JSONParser();

    private ProgressDialog pDialog;

    private static final String LOGIN_URL = "http://10.0.3.2/modify_visit_details.php";

    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";


    @Override
    protected void onPreExecute() {
        pDialog = new ProgressDialog(modify_visit_details.this);

    }

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

        try {

            HashMap<String, String> params = new HashMap<>();
            params.put("event_date", args[0]);
            params.put("event_location", args[1]);
            params.put("organisation_name", args[2]);

            Log.d("request", "starting");

            JSONObject json = jsonParser.makeHttpRequest(
                    LOGIN_URL, "POST", params);

            if (json != null) {
                Log.d("JSON result",json.toString());
                return json;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(JSONObject json) {

        int success = 0;
        String message = "";

        test1 = eventdate.getText().toString();
        test2 = item;
        test3 = orgname2.getText().toString();

        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
        }


        else if (json != null) {
            {

                JSONArray data= null;
                try {
                    data = json.getJSONArray("details");
                    JSONObject obj=data.getJSONObject(0);
                    Topic1=obj.getString("topic1");

                    Log.d("test1",test1);//Printing all correctly in LOG
                    Log.d("test2",test2);
                    Log.d("test3",test3);
                    Log.d("Topic1",Topic1);


                } catch (JSONException e) {
                    e.printStackTrace();
                }



            }


        }
        else {
            Toast toast = Toast.makeText(
                    modify_visit_details.this,
                    "json null",
                    Toast.LENGTH_LONG
            );
            ViewGroup group = (ViewGroup) toast.getView();
            TextView messageTextView = (TextView) group.getChildAt(0);
            messageTextView.setTextSize(20);
            messageTextView.setTypeface(Typeface.SERIF);
            toast.show();

            if (success == 1) {
                Log.d("Success", message);
            } else {
                Log.d("Failure", message);
            }
        }
    }

}

您不能只访问这些值,因为这些值是在一段时间后从数据库检索的。使用AsyncTask时,您可以存储其他Runnable并在postExecute()中运行它。

您正在创建一个独立于UI线程的线程

因此,在这段代码之后:

new PostAsync().execute(evtdt,evtloc,orgnm);
您将有两个独立的线程(Ui线程,您的异步任务)

因为,您的主UI线程比您的AsyncTask执行快,所以您的变量当然是空的

此日志将显示为空:

 Log.d("test1outside",test1);//Showing NULL
 Log.d("test2outside",test2);//Showing NULL
 Log.d("test3outside",test3);//Showing NULL
 Log.d("Topic1outside",Topic1);//Showing NULL
总之,如果您想查看您的变量,请输入日志

protected void onPostExecute(JSONObject json)

如前所述,使用回调接口。由于Asynctask是异步的,因此在任务完成之前打印日志值。为什么在调用Asynctask后立即需要这些test1、test2等?正如我从您的代码中看到的,您似乎在
onPostExecute
?@AADTechnical我想将test1 test2等的值发送到其他活动ok,我想@BorislavKamenov提到的接口解决方案是您的正确选择……在这种情况下!这些日志已经存在于onPostExecute中,根据OP,它是正确的OK,因此定义接口可能是问题的解决方案,正如前面的评论所说,@uelordi You是绝对正确的。但是如果我想访问onPostExecute之外的值,那么实际值不是空值呢。@somyaArora,您可以复制全局辅助变量中的值以访问,也可以使用标志指示是否准备就绪。不等待准备就直接从其他线程访问变量不是一个好主意,因为这是一种未定义的行为。
protected void onPostExecute(JSONObject json)