📜 ⬆️ ⬇️

Using a delegate to get data from AsyncTask

Hello!

I want to share one little trick for novice android writers. This trick is as old as the world, and I myself used it many times before, but since my main work with android is in no way connected, then after years of years I forgot it and here I had to recall it urgently the other day.


It took me a micro-application, the task of which is simple to disgrace: get data from a web service and display it on the screen as a list. It would seem, what could be easier? But there is one “but”, more precisely, these “but” even a few.
')
First, all operations that may take an indefinite amount of time (and this includes all operations with the network) should be executed in a separate thread. The Android SDK provides us with at least two solutions to this issue, one of which (and in my opinion the most correct one) is AsyncTask . So, this is understandable. Go ahead.

The second and most important problem is how to return the result of working with the network to our main stream in order to show it on the screen? Well, let's understand ... What do we know about AsyncTask from what could help us? And we know that its onPostExecute method runs in the main thread! This fact gives us at least two solutions to the problem. Number one time - we can simply transfer our graphic control (in my case it is a ListView) to AsyncTask with a parameter and fill it with data on onPostExecute . This option is most often found on the Internet as an example of solving our problem. And number two — an option that gives us more flexibility — is the use of a delegate. Of course, there are also several options, but I will consider only one of them, which I like the most.

It's time to move from words to deeds!

So, first we will create an interface in which there will be only one method:
public interface GetDataListener { void onGetDataComplete(JSONArray result); } 


Now we implement this interface in our activity:
 public class MyActivity extends Activity implements GetDataListener { private ListView list; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); list = (ListView) findViewById(R.id.lvData); } public void onGetDataComplete(JSONArray responce) { ... //    ListView ... } } 


Well, now it's time for AsyncTask:
 public class GetData extends AsyncTask<Void, Void, JSONArray> { private final static String SERVICE_URI = "http://moi.service.com/service.php"; private GetDataListener listener; GetData(GetDataListener listener) { this.listener = listener; } protected void onPostExecute(JSONArray result) { listener.onGetDataComplete(result); } protected JSONArray doInBackground(Void... params) { return CallService(); } private JSONArray CallService() { JSONArray records = null; ... //      ... return records; } } 

Pay attention to the designer! Here we get our delegate and save it.

Now everything is almost ready, it remains only to start our mechanism from our activity:

 new GetData(this).execute(); 


As you can see nothing revolutionary, pure and simple. I hope that I saved some time.

Source: https://habr.com/ru/post/144400/


All Articles