【问题标题】:Need an example showing how to do async HTTP requests需要一个示例来展示如何执行异步 HTTP 请求
【发布时间】:2010-02-23 08:45:56
【问题描述】:

我正在使用 Web 服务,因此我想使用异步线程进行 HTTP 身份验证请求,稍后再使用另一个线程在我的主线程运行时发出额外的服务请求。

希望看到一个很好的例子来说明如何做到这一点,以及如何在主应用程序中以某种方式显示繁忙的消息。主应用程序如何知道线程何时结束?如果我的线程遇到异常怎么办?

HTTP 请求稍后发送,使用第一个身份验证请求设置的相同 cookie,那么后面的请求是否会选择相同的 cookie 并正常工作?

【问题讨论】:

    标签: android http multithreading asynchronous task


    【解决方案1】:

    【讨论】:

      【解决方案2】:
      【解决方案3】:

      我编写的 AndroidAsync 库可以自动处理这个问题,它将在后台运行并重新调用到 UI 线程:

      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);
          }
      });
      

      【讨论】:

        猜你喜欢
        • 2018-05-08
        • 1970-01-01
        • 2021-03-03
        • 1970-01-01
        • 1970-01-01
        • 2018-12-05
        • 2018-04-27
        • 1970-01-01
        • 2012-12-11
        相关资源
        最近更新 更多