-3

Can i do network connections in doInBackground of AsyncTask. If not what to use for this.

As per i know i can use intentService for long running operation but this will lead some complexity in code.

Please suggest for the same.

Durgesh
  • 101
  • 7

4 Answers4

0

You can use volley for network data.

e-shfiyut
  • 3,538
  • 2
  • 30
  • 31
0

you can run network connections in doInBackground. but using libraries such as volley is better for you. you can learn about volley in :

http://developer.android.com/training/volley/index.html

volley run network connection in background and send result to you in main thread and you don't need to worry about network thread.

Reza Azami
  • 88
  • 1
  • 7
0

Short answer, you can, your network operation will run on the background.

Long answer, I would not recommend using an AsyncTask for that purpose. The main reason:

  • AsyncTasks do not handle activity lifecycle and configuration changes. This means that if you fire an async task when your activity is created for example, if the user rotates the screen, firing configuration changes, you will have an orphan AsyncTask running (potentially leaking memory and/or crashing the application once it is done executing) and will potentially launch a new one as well.

As other replies have highlighted, depending on your use case you might be able to use Volley.

unbekant
  • 1,555
  • 22
  • 31
0

The short answer is: Yes.

The longer answer is that network communication is always hard, and while the code is definitely simpler when using AsyncTask compared to other methods, the problems themselves are identical.

Elias Mårtenson
  • 3,820
  • 23
  • 32