Best way to send HTTP GET requests ansynchronously in Android?

后端 未结 2 742
暖寄归人
暖寄归人 2021-01-31 05:59

In an Android app, I have to make multiple GET requests to a URL in order to transmit data to an external server (that\'s how the third party API works).

Data comes in s

2条回答
  •  温柔的废话
    2021-01-31 06:41

    You could do your own async HTTP GET calls using AsyncTask but I would recommend against it unless you're doing it from a learning point of view. If you want a nice, clean and stable solution I'd suggest that you use the well known Android Asynchronous Http Client 3rd party library. From the site:

    "An asynchronous callback-based Http client for Android built on top of Apache’s HttpClient libraries. All requests are made outside of your app’s main UI thread, but any callback logic will be executed on the same thread as the callback was created using Android’s Handler message passing."

    Making a GET is as easy as:

    AsyncHttpClient client = new AsyncHttpClient();
    client.get("http://www.google.com", new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String response) {
            System.out.println(response);
        }
    });
    

提交回复
热议问题