1

I am currently having trouble getting a value from an AsyncTask that gets data from a JSON connection. I have looked at a few examples, but mostly I have only seen posting results from AsyncTask.

First I have an object called Dog that only has a String, Name. I am trying to get the Name of the dog from the server. Using the following code in my oncreate, I start the DogAsyncTask while assing in an URL called n and a Dog d_in.

    Dog d_in = new Dog("DogName");
    DogAsyncTask task = new DogAsyncTask(d_in);
    String n = "www.dog.com";
    task.execute(n);
    Log.e("Out", d_in.getName());

My AsyncTask is as follows:

 private class DogAsyncTask extends AsyncTask<String, Void, String> {

    Dog d = null;

    DogAsyncTask(Dog d){
        this.d = d;
    }

    ProgressDialog mProgressDialog;
    @Override
    protected void onPostExecute(String result) {
        d.setName(result);
        Log.e("Dog", d.getName());
        mProgressDialog.dismiss();
    }

    @Override
    protected void onPreExecute() {
        mProgressDialog = ProgressDialog.show(AsyncTestActivity.this, "Loading...", "Data is Loading...");
    }

    @Override
    protected String doInBackground(String... name) {
        //Go to url = name and then gets String below.
        String outfromjson = "new dog name";  //This will be a function that gets a name from JSON
        return outfromjson;
    }
}

I tried using something like Log.e("Out", task.d.getName()); but I keep getting the default name of the dog which is "DogName". How to I carry values out of AsyncTask?

d.mc2
  • 1,129
  • 3
  • 16
  • 31

2 Answers2

1

You can do the following,

Declare and implement an interface in your activity and then use it's methods as a callback from the onPostExecute method.

R.daneel.olivaw
  • 2,681
  • 21
  • 31
1

OK, The thing what is happening here is:

You want to pass Dog as reference (pointers as in C / C++ / Objective-C), and you want to reflect change in original variable using other variable.

but keep in mind that Java doesn't pass object by reference, Java passes objects by value (only value is transferred, another object is created and assigned value).

So what you can do is you can assign that result-holder object again as a value to the original object.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • Would you know how to get the result (Object data) out of the new DataDownloadListener()? – d.mc2 Jan 21 '12 at 10:21
  • as I've implemented [here](http://stackoverflow.com/a/7618705/593709), I pass the data to `listener.downloadedSuccessfully(data)` and in implementation I get that data where I've commented `// handler result`, you can get the result there.. :) – Adil Soomro Jan 21 '12 at 10:34
  • I am still a little confused. In my activity, after the last line: getdata.execute("");, I want to return data; upwards out of a function, how can I get Object data from dataDownloadedSuccessfully(Object data) and return it outwards? – d.mc2 Jan 21 '12 at 10:43
  • You can set that Object data to any class level variable. or you can set Text to any TextView or manipulate as you wish.. – Adil Soomro Jan 21 '12 at 14:29
  • even we can use our own interface to get return value from asyncTask. i have just posted example for the same on my blog. You can refer it on below link with Sample Code. [http://smartphonebysachin.blogspot.in/2012/11/how-to-return-value-from-async-task-in.html]http://smartphonebysachin.blogspot.in/2012/11/how-to-return-value-from-async-task-in.html – Sachin Shelke Nov 01 '12 at 08:03
  • @SachinShelke: your solution is identical to [this post](http://stackoverflow.com/a/7618705/593709), see the note and comments on this post to have some idea about assigning another listener to `AsyncTask` – Adil Soomro Nov 01 '12 at 08:13