How to return a result from an asynchronous task

I use async tasks to hit the web server and update the controls using the result. This has flaws, namely, what makes the asynchronization methods specific to the controls and stops me again with the return string.

How to return a returned string from an asynchronous call to onPostExecute? What should I call it? I can't seem to get my code to do this. There should be no problems with stream processing, since I have a dialog that freezes the user interface until the task is completed.

My typical asyncTask code is as follows

class GetDataFromServer extends AsyncTask<String, String, String>
{
     * */
    // Progress Dialog
    private ProgressDialog qDialog;
    private Context context;
    private String dialogString;
    private ArrayList<String[]> newLoginResult;

    // JSON parser class
    String url_newGame ="http://xxxxxx.php";

    public myAsyncMethos(String dialogMessage, Context con)
    {
        this.qDialog =  new ProgressDialog(con);
        this.dialogString = dialogMessage;
        this.context = con;
    }

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        qDialog = new ProgressDialog(this.context);
        qDialog.setMessage(this.dialogString);
        qDialog.setIndeterminate(false);
        qDialog.setCancelable(false);
        qDialog.show();
    }

    @Override
    protected JSONObject doInBackground(String... args)
    {
        //MAKE SERVER CALL and cast to JSONOBject

        return jsonNewUser;
    }

    public void onPostExecute(JSONObject jsonString)
    {

         // dismiss the dialog after getting response
        qDialog.dismiss();
//I WANT TO RETURN A STRING HERE BUT KEEP GETTING SYNTAX ERRORS BEFORE RUNTIME


    }
}
+4
source share
4 answers

, , onPostExecute , .

class GetDataFromServer extends AsyncTask<String, String,JSONObject>
{
    // Progress Dialog
    private ProgressDialog qDialog;
    private Context context;
    private String dialogString;
    private ArrayList<String[]> newLoginResult;
    private InformComplete myCallback;

    public GetDataFromServer(String dialogMessage, Context con,InformComplete callback)
    {
        this.qDialog =  new ProgressDialog(con);
        this.dialogString = dialogMessage;
        this.context = con;
        this.myCallback=callback;
    }

    @Override
    protected void onPreExecute()
    {
        // set up your dialog
    }

    @Override
    protected JSONObject doInBackground(String... args)
    {
        JSONObject jsonNewUser=new JSONObject();
        return jsonNewUser;
    }

    public void onPostExecute(JSONObject jsonString)
    {
        qDialog.dismiss();
        myCallback.PostData(jsonString);
    }
    public interface InformComplete
    {
        public void PostData(JSONObject result);
    }
}

- ...

    private void callTheAsyncThing
    {
        GetDataFromServer gds=new GetDataFromServer("please wait", this, letMeKnow);
        gds.execute(params);
    }

    private InformComplete letMeKnow=new InformComplete()
    {
        public void PostData(JSONObject result)
        {
            // we now have the data in the calling class
        }
    };
+4

AsynTask, void. , var foe . ... `

class GetDataFromServer extends AsyncTask<String, String, String>
{
     * */
    // Progress Dialog
    private ProgressDialog qDialog;
    private Context context;
    private String dialogString;
    private ArrayList<String[]> newLoginResult;
    private String value;

    // JSON parser class
    String url_newGame ="http://xxxxxx.php";

    public myAsyncMethos(String dialogMessage, Context con)
    {
        this.qDialog =  new ProgressDialog(con);
        this.dialogString = dialogMessage;
        this.context = con;
    }

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        qDialog = new ProgressDialog(this.context);
        qDialog.setMessage(this.dialogString);
        qDialog.setIndeterminate(false);
        qDialog.setCancelable(false);
        qDialog.show();
    }

    @Override
    protected JSONObject doInBackground(String... args)
    {
        //MAKE SERVER CALL and cast to JSONOBject

        return jsonNewUser;
    }

    public void onPostExecute(JSONObject jsonString)
    {

         // dismiss the dialog after getting response
        qDialog.dismiss();
        value = "Whatever you want";
    }
    public void setValue(String value){
         this.value=value;
    }
    public String getValue(){
         return this.value;
    }
}`

. -. Btw .

+1

The onPostExecute method is not a programmer call caused by an AsyncTask instance. If you want to return the result from the async task, you can send the parameter to GetDataFromServer and in the onPostExecute method you give it a new value, And then you can use Handler to send the message.

0
source

Your return JSONObject in doInBackground when you try to get a string in the onPostExecute method.

public void onPostExecute(JsonObject jsonString)
{
     // dismiss the dialog after getting response
     qDialog.dismiss();
     //I WANT TO RETURN A STRING HERE BUT KEEP GETTING SYNTAX ERRORS BEFORE RUNTIME
}
-1
source

All Articles