简体   繁体   English

Android:处理同步网络呼叫

[英]Android: Handling synchronous network calls

I have an android library that makes a significant amount of synchronous network calls, I intend to use this library in my app. 我有一个Android库,可以进行大量的同步网络调用,我打算在我的应用程序中使用这个库。 For example, when the app launches I need to display a list of users, a list of usergroups and general info. 例如,当应用程序启动时,我需要显示用户列表,用户组列表和常规信息。 For this I already have a main activity which holds three fragments(two listfragments for the list and one normal fragment) for the three things to display, the data for the user list and list of usergroups are from synchronous network calls in the library mentioned earlier. 为此我已经有一个主活动,它包含三个片段(列表的两个列表片段和一个普通片段),用于显示三个内容,用户列表的数据和用户组列表来自前面提到的库中的同步网络调用。 My question is, what is the proper(recommended) way to use these synchronous library calls to fetch the data to be displayed by the fragments. 我的问题是,使用这些同步库调用来获取片段显示的数据的正确(推荐)方法是什么。 From my understanding there are 3 major ways of doing this. 根据我的理解,有三种主要方法可以做到这一点。

  1. Using the AsyncTask class, so for the library methods getUserList(...) I'll have to create a dedicated AsyncTask class. 使用AsyncTask类,因此对于库方法getUserList(...),我将不得不创建一个专用的AsyncTask类。

     public class FetchTask extends AsyncTask<Void, Void, Boolean> { private List<User> users; protected Boolean doInBackground(Void... params) { users = getUserList(...); } protected void onPostExecute(final Boolean success) { //inform user list fragment that user data has been fetched successfully } 

    } }

    If I use this method it means I will have to create an asyncTask class for every network call made(which is a significant amount) and have it communicate with the UI thread on completion 如果我使用这个方法,这意味着我将不得不为每个网络调用创建一个asyncTask类(这是一个很大的数量)并让它在完成时与UI线程通信

  2. Directly spinning off a thread to wrap the synchronous call 直接旋转线程以包装同步调用

     public class UserListFragment extends ListFragment { private List<User> users; private ArrayAdapter<User> userAdapter .... protected void OnCreate(Bundle savedInstance) { super.OnCreate new Thread(new Runnable(){ public void run(){ users = getUserList() //notify array adapter that data has changed } }) } } 

This method also requires that I repeat this for every synchronous method call I make and can easily get unwieldy. 这个方法还要求我为每个同步方法调用重复这个,并且很容易变得笨拙。

  1. The last method is using the Threadpoolexecutor, I must admit I don't yet fully understand this method. 最后一种方法是使用Threadpoolexecutor,我必须承认我还没有完全理解这种方法。

Again, my question is what is the preferred method of achieving this goal? 同样,我的问题是实现这一目标的首选方法是什么? Is it any of the 3 that I've listed? 这是我列出的3个中的任何一个吗? Thanks. 谢谢。

You should go with Volley Library for network operation because volley maintain all the necessary method which you will need to call a perfect network api. 你应该和Volley Library一起进行网络操作,因为volley会保留所有必要的方法,你需要调用一个完美的网络api。 Click Here 点击这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM