1

I am a novice Android programmer experimenting with AsyncTask and performing HTTP requests. My goal is to be able to perform the HTTP request using AsyncTask. My AsyncTask is below:

private class DownloadFilesTask extends AsyncTask<String, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected List<Book> doInBackground(String... params) {
        return Network.getBookData(params[0]);
    }
    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
    }
}

There is an error that says in the doInBackground(String... params) method, there is an attempt to use an incompatible return type. How can I fix this and still have the method return a List<Book>?

  • What's your problem? There's no general problem with returning a list, just remember that the return value is passed into onPostExecute. Its not returned from execute – Gabe Sechan Dec 18 '16 at 03:29
  • 1
    read here first -> http://stackoverflow.com/questions/25647881/android-asynctask-example-and-explanation – Charuක Dec 18 '16 at 03:57
  • Thanks Charuka. Updated question based on that post. –  Dec 18 '16 at 04:18

1 Answers1

0

You can either pass the dataUrl parameter into the constructor of the task or as the parameter for the task. The way you use the AsyncTask will change slightly depending on which you use.

As a constructor parameter:

new DownloadFilesTask(someUrl).execute(null);

As a task parameter

new DownloadFilesTask().execute(someUrl);

You should override onPostExecute(List<Book> result) in your Task class in order to do something with the HTTP results on the UI thread, such as rendering the Book objects in a list.

Sean Amos
  • 59
  • 2
  • I tried both the constructor parameter and the task parameter and both don't work. –  Dec 18 '16 at 04:03
  • What do you mean they don't work? Is this a compilation or runtime error? Can you provide the code for `Network.getBookData`? – Sean Amos Dec 18 '16 at 04:22
  • Ok. Here it is: –  Dec 18 '16 at 04:27
  • public static List getBookData(String dataUrl) { URL url = createUrl(dataUrl); String jsonResponse = null; try { jsonResponse = makeHttpRequest(url); } catch (IOException e) { Log.e(LOG_TAG, "Problem making the HTTP request.", e); } // Extract relevant fields from the JSON response and create a list of {@link Earthquake}s List books = extractFeatureFromJson(jsonResponse); // Return the list of {@link Earthquake}s return books; } –  Dec 18 '16 at 04:28
  • 1
    I would recommend reading up on what the generic types on AsyncTask represent. https://developer.android.com/reference/android/os/AsyncTask.html The third type parameter is the return type of the `doInBackground` method, which right now is `Void`. So you should update your class definition to `public class DownloadFilesTask extends AsyncTask>` – Sean Amos Dec 18 '16 at 04:34
  • That fixed the problem. Thanks a lot!! –  Dec 18 '16 at 04:40