Need an example showing how to do async HTTP requests

后端 未结 3 1817
轻奢々
轻奢々 2021-02-09 21:33

Im using a web service, so I want to use an async thread for the HTTP authentication request and another thread later to make additional service requests while my main thread ru

相关标签:
3条回答
  • 2021-02-09 22:16

    AndroidAsync library I wrote to handle this automatically, it will run in the background and reinvoke onto the UI thread:

    https://github.com/koush/AndroidAsync

    // url is the URL to download. The callback will be invoked on the UI thread
    // once the download is complete.
    AsyncHttpClient.getDefaultInstance().get(url, new AsyncHttpClient.StringCallback() {
        // Callback is invoked with any exceptions/errors, and the result, if available.
        @Override
        public void onCompleted(Exception e, String result) {
            if (e != null) {
                e.printStackTrace();
                return;
            }
            System.out.println("I got a string: " + result);
        }
    });
    
    0 讨论(0)
  • 2021-02-09 22:20

    Even better look here for the async part: Is there an accepted best-practice on making asynchronous HTTP requests in Android?

    0 讨论(0)
  • 2021-02-09 22:26

    Look at here How to execute web request in its own thread?

    0 讨论(0)
提交回复
热议问题