2010-02-23 87 views
6

即时通讯使用Web服务,因此我希望使用异步线程进行HTTP身份验证请求,并且稍后使用另一个线程在运行主线程时发出其他服务请求。需要显示如何执行异步HTTP请求的示例

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

HTTP请求稍后发送,使用由第一个认证请求建立的相同的cookie,那么后面的请求会拿起相同的cookies并且工作吗?

回答

1

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