Today I had an exam where had to deliver some database data to Android application and I created socket protocol which I used to call server with AsyncTask class to deliver me the domain specific requested data. But my teacher told me that the AsyncTask class is obsolete and there is another way to solve the problem. Could you tell me the way I could solve the problem without using AsyncTask class, domain classes and soket communication.
Thanks
AsyncTask
is not obsolete (nor is it deprecated), but it's probably not a good choice for persistent socket communication. AsyncTask
is better suited to relatively short-lived operations, including ones that make UI modifications to show progress or upon completion since the threading is handled for you. Because you can use AsyncTask
to modify the UI, most people associate the AsyncTask
with their Activity
or some of its View
s, which can be a problem since any configuration change may cause the Activity
to be destroyed and recreated, but you will still have an AsyncTask
living in memory holding on to the entire Activity
context.
For something like a persistent socket, you are better off using a Service
, which is a separate application component that won't go through configuration changes like Activity
s do. You can also manage its lifecycle differently, whereas the Activity
lifecycle is dictated primarily by user interaction.
You can use some http request library like "Volley" or "OkHttp" (my favorite)
Create your own server API to communicate like this:
Phone send http request to an web API -> Web API receive the request, process and call Database
Database responses and send the data to the Web API -> Web API get the database response and send to your Phone -> Your phone receive the data
You can use Json or XML for the data transfer (Prefer Json for the simplicity)
According to the android wiki, "AsyncTask" is not deprecated !
You need to create state full connection using TCP / UDP protocol, both implement scoket programming then you will be able to pass data to android client. other work around is to use Google messaging (GCM) through GCM you can send data to your android application. OR you can use Open fire for socket programming.