0

I need to request data from webservice. I've implemented the following AsyncTask to do that:

class LoadTagsTask extends AsyncTask<Void, Void, String> {
    private String url = "http://webservice.com/rest/tagsManager/search";
    ProgressBar loader;

    @Override
    protected void onPreExecute() {
        loader = (ProgressBar) TextsListActivity.this.findViewById(R.id.textLoadingProgressBar);
        loader.setVisibility(TextView.VISIBLE);
    }

    @Override
    protected String doInBackground(Void... params) {
        HttpURLConnection c = null;
        try {
            c = (HttpURLConnection) new URL(url).openConnection();
            InputStream in = c.getInputStream();

            StringBuilder stringBuffer = new StringBuilder();
            final char[] charBuffer = new char[8 * 1024];
            BufferedReader reader = new BufferedReader(new InputStreamReader(in), charBuffer.length);
            int read;
            while ((read = reader.read(charBuffer)) != -1) {
                stringBuffer.append(charBuffer, 0, read);
            }

            reader.close();
            return stringBuffer.toString();
        } catch (Exception e) {
            return "";
        } finally {
            if (c != null) {
                c.disconnect();
            }
        }
    }

    @Override
    protected void onPostExecute(String tags) {
        loader.setVisibility(TextView.GONE);
        TextsListActivity.this.showTagsSelectorActivity(tags);
    }
}

Does it make sense to replace it with AsyncTaskLoader? I've read that they can be used interchangeably, but AsyncTaskLoader does a bit better job. Is is applicable in my this case?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • Read this: http://stackoverflow.com/questions/7120813/asynctaskloader-vs-asynctask – Shahzeb May 20 '15 at 07:19
  • @Shahzeb, thanks! I guess what you're saying is that I't be a good idea to replace it, correct? :) – Max Koretskyi May 20 '15 at 07:21
  • well, as long as you are comfortable with it :) – Shahzeb May 20 '15 at 07:26
  • I'm not, but I can get if needed :). That's basically what this question is about - should I spend extra time to learn how to employ `AsyncTaskLoader` or in my particular case it won't get me significant advantage? – Max Koretskyi May 20 '15 at 07:30
  • 1
    This pretty explains your case: http://stackoverflow.com/questions/15079948/guideline-to-choose-among-asynctaskloader-and-asynctask-to-be-used-in-fragment – Shahzeb May 20 '15 at 07:36
  • 1
    In my opinion, you do not need AsyncTaskLoader as such – Shahzeb May 20 '15 at 07:37
  • @Shahzeb, great, thanks a lot for you help! I'll also read through the links you provided. Best! – Max Koretskyi May 20 '15 at 07:38

1 Answers1

4

I think you should replace AsyncTask with Retrofit.

It encapsulates low-level logic like managing threads and http conection.

Using Retrofit makes your code cleaner and allow you to focus only on the data which the backend serves.

Retrofit supports:

  • synchronus, asynchronus and observables kinds of execution
  • integrated by default with gson
  • request interceptors
  • nice testing with mocking
klimat
  • 24,711
  • 7
  • 63
  • 70
  • 1
    Thanks a lot for you suggestion! Indeed, that sounds like the most befitted thing for my task. PS. Although for some reason some of my questions get downvoted, I still get valuable advice because of the people like you :). Best! – Max Koretskyi May 20 '15 at 08:32
  • @mklimek I was wondering if you could help me with this question http://goo.gl/d5opg5 . It's not one of those "fix-this-for-me" questions, I promise. – Axel Jun 22 '15 at 03:31
  • GSON is no longer included, but installing it should be trivial – Mazen Elkashef Feb 24 '16 at 09:38