5

I have been using async tasks to hit a web server and updating controls using the result. This has drawbacks, namely it makes the async methods specific to controls and stops me using the returned string again.

How do i return the resulting string from a async call onPostExecute? How do i call it? I cant seem to get my code able to do that. There should be no issues with threading as i have a dialog that freezes the UI until job is done.

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


    }
}
Fearghal
  • 10,569
  • 17
  • 55
  • 97

4 Answers4

4

I would personally add a callback to your class, then once onPostExecute is run, fire off your callback to the listener on the main class.

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);
    }
}

Then from your calling class, you'd have something like this...

    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
        }
    };
Rich S
  • 3,248
  • 3
  • 28
  • 49
1

You can't return a value in methods from AsynTask cause it used to return a void element. So, you can instance global var foe example, and set the value to it. Like... `

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;
    }
}`

And then use it. There is one way to return something. Btw you can't change return.

Jnmgr
  • 169
  • 1
  • 13
  • that variable is not global right? It cant be accessed from other classes can it? – Fearghal May 18 '15 at 11:12
  • Is not 100% "global", my apologize. When I said "global", I wanted to say in the same class within other methods. Global as you said wherever class were, is public. Protected, if it is in the same package. Private only the same class. Actually, should do getter and setter for that var to set and get values. I did not type it in this example, cause usually makes pojo's class. – Jnmgr May 18 '15 at 13:27
  • I have edited to show how you can do setter and getter; as i said it makes in pojo's class and not in the same class. Indeed, create another class with the AyncTask is the best way to do if you want to be "purist" as you did. – Jnmgr May 18 '15 at 13:34
0

the method of onPostExecute is not Programmer call,it called by AsyncTask instance.if you want to return a result from a async task,you can send a parameter into GetDataFromServer ,and in method of onPostExecute,you give him the new value,and then,you can use Handler to send a message.

-1

Your return JSONObject in doInBackground method while you try to get string in 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
}
Ed Holloway-George
  • 5,092
  • 2
  • 37
  • 66
Ahsan Hameed
  • 31
  • 1
  • 5