-2

is it possible to grab the return value of the doInBackground()-method?

JSONArray response = new SupportFunctions.executeURLWithResponse().execute(url);

It tells me:

Description Resource    Path    Location    Type
Type mismatch: cannot convert from AsyncTask<String,Void,JSONArray> to JSONArray          ContactFunctions.java /BuddyCheck/src/com/pthuermer/buddycheck    line 138    Java Problem

Something like this?

user3432220
  • 101
  • 6
  • 1
    did you try reading the documentation ? – njzk2 Apr 07 '14 at 22:57
  • 1
    If the task is in a separate file you can use an `interface`. [Here is an example](http://stackoverflow.com/questions/18517400/inner-class-can-access-but-not-update-values-asynctask/18517648#18517648) otherwise you can update the value or call a function to set the value in `onPostExecute()` – codeMagic Apr 07 '14 at 23:01
  • codeMagic is right, you need to `@Override` `void onPostExecute(Result result)` – motoku Apr 07 '14 at 23:07
  • and then? where is the result? I cant return something out of onPostExecute, right? – user3432220 Apr 07 '14 at 23:08
  • See my linked answer about using an interface – codeMagic Apr 07 '14 at 23:14

2 Answers2

1

is it possible to grab the return value of the doInBackground()-method?

Not in the manner that you are trying.

There is a get() method you can call to block the current thread and wait for the asynchronous work to complete. That usually is not what you want -- you specifically want the work to be done asynchronously (otherwise, why have an AsyncTask?).

Move you code that needs the JSONArray response either into onPostExecute() itself or into some other method that will be executed by onPostExecute().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'm using asynctask because its the only way to grab a JSON response from an URL, right? – user3432220 Apr 07 '14 at 23:01
  • @user3432220 No. You need (should) to do it on a background thread but that doesn't mean you have to use an `AsyncTask`, though it is often used for this type of thing. You should read about threading [here](http://android-developers.blogspot.com/2009/05/painless-threading.html) and [here](http://developer.android.com/guide/components/processes-and-threads.html) – codeMagic Apr 07 '14 at 23:02
  • @user3432220: You need to use a background thread to "grab a JSON response from an URL". An `AsyncTask` is a wrapper around a background thread. But the reason why you need a background thread is so that you do not tie up the main application thread waiting for network I/O. Process the results of the `AsyncTask` background work in `onPostExecute()` -- that's why it is there. – CommonsWare Apr 07 '14 at 23:03
  • would it be easier to grab the jsonarray response from a background thread? – user3432220 Apr 07 '14 at 23:03
  • @user3432220 you are using a background thread. That's what `doInBackground()` is. The `AsyncTask` class just also has methods to update the Ui. – codeMagic Apr 07 '14 at 23:04
  • okay, so thats confusing. would the thread possible return the array? – user3432220 Apr 07 '14 at 23:06
0

You can make response a class or global variable then set the variable when the async task finishes. Most of what you should be doing should be in your override of onPostExecute.

Get returned JSON from AsyncTask

Community
  • 1
  • 1
Dave S
  • 3,378
  • 1
  • 20
  • 34