Example: Android Bi-Directional Network Socket Using Asynctask

Reading data from socket using AsyncTask in android studio

The AsyncTask class has two methods of importance for your problem, doInBackground(Params...) and onPostExecute(Result).

You are using a DataInputStream to retrieve the data from your socket using its readUTF method. The readUTF method returns a String. I am not sure what data you are passing to your socket to initiate the request/response action but whatever that data type is you need to override doInBackground method with it. For example lets say you are passing a string of data to the server using the socket. Your doInBackground method would look something like

@Override
protected String doInBackground(String... data) {
/* set up the socket/DataInputStream, etc. and whatever else you need
in the background */
DataInputStream in = ...;
return in.readUTF();
}

The return type is a String just because that is the data returned by the readUTF method and thus I assume you want a String returned to the UI thread.

Next you need to override the onPostExecute method as follows

@Override
protected void onPostExecute(String result) {
// do whatever needs to be done on the UI thread with the result
}

Your subclassed AsyncTasks would be defined as

class SocketTask extends AsyncTask<String, Void, String> {
...
}

Asynchronous Socket Reading

Socket.getInputStream().read() can be used in your AsyncTask. It blocks.

Socket.getOutpuStream().write() can be put in your main thread and will work fine if the read is blocking in another thread.

Better way to create bidirectional communication

You can use Threads in any android version.

The right way to do what you want, would be to create a Service ( http://developer.android.com/reference/android/app/Service.html ) and use separate Threads in the Service to send and to recieve data.

Android: Best practice to read data from socket

  1. What is difference between these two ways of reading?

The second approach used BufferedReader, which has a internal buffer mechanism, which make you write less code.


  1. First one was written as AsyncTask while second one was intended to run as separate thread. Which one is correct approach?

AsyncTask is a wrapper of Thread, using AsyncTask can do network operation in the background thread and publish result int the ui thread.AsyncTask also manages the Thread pool, in some cases, you need not create a new thread every time. It is recommended to use AsyncTask in Android.


  1. Is there any better way to read from socket? (e.g. using non-blocking sockets, callbacks, using any popular third party library etc.)

You can use Square's okio, it is a better IO library for java, and has Buffer support.

proper way of using sockets in android app

Since network connections can take up time and make your app unresponsive if done on the UI thread, you should probably do them in an AsyncTask. If your AsyncTask is sufficiently complex it could be worth implementing in its own java file as a proper public class.

This answer would give you the basic skeleton. AsyncTask makes it easy to interact with the UI at the beginning and the end of your background loading, if you needed to show some progress indication.

Thread tends to be a worse choice as you are still left with the problem of UI interaction when your thread completes as well as making sure that the thread doesn't continue its work after your activity finishes; an AsyncTask can be also be cancelled and then onCancelled will get executed instead of onPostExecute.

Android: Trying to use AsyncTask to talk to web server

use

new SendTextOperation().execute();

instead of this

new SendTextOperation().execute("");


Related Topics



Leave a reply



Submit