java – 如何在Android上建立异步URL连接?

前端之家收集整理的这篇文章主要介绍了java – 如何在Android上建立异步URL连接?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我使用以下类连接到我的Web服务.我想让这个异步.我怎样才能做到这一点?

  1. package org.stocktwits.helper;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.client.ClientProtocolException;
  9. import org.apache.http.client.HttpClient;
  10. import org.apache.http.client.methods.HttpGet;
  11. import org.apache.http.impl.client.DefaultHttpClient;
  12. import org.json.JSONException;
  13. import org.json.JSONObject;
  14. import android.util.Log;
  15. public class RestClient{
  16. private static String convertStreamToString(InputStream is) {
  17. /*
  18. * To convert the InputStream to String we use the BufferedReader.readLine()
  19. * method. We iterate until the BufferedReader return null which means
  20. * there's no more data to read. Each line will appended to a StringBuilder
  21. * and returned as String.
  22. */
  23. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  24. StringBuilder sb = new StringBuilder();
  25. String line = null;
  26. try {
  27. while ((line = reader.readLine()) != null) {
  28. sb.append(line + "\n");
  29. }
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. } finally {
  33. try {
  34. is.close();
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. return sb.toString();
  40. }
  41. /* This is a test function which will connects to a given
  42. * rest service and prints it's response to Android Log with
  43. * labels "Praeda".
  44. */
  45. public static JSONObject connect(String url)
  46. {
  47. HttpClient httpclient = new DefaultHttpClient();
  48. // Prepare a request object
  49. HttpGet httpget = new HttpGet(url);
  50. // Execute the request
  51. HttpResponse response;
  52. try {
  53. response = httpclient.execute(httpget);
  54. // Examine the response status
  55. Log.i("Praeda",response.getStatusLine().toString());
  56. // Get hold of the response entity
  57. HttpEntity entity = response.getEntity();
  58. if (entity != null) {
  59. // A Simple JSON Response Read
  60. InputStream instream = entity.getContent();
  61. String result= convertStreamToString(instream);
  62. // A Simple JSONObject Creation
  63. JSONObject json=new JSONObject(result);
  64. // Closing the input stream will trigger connection release
  65. instream.close();
  66. return json;
  67. }
  68. } catch (ClientProtocolException e) {
  69. // TODO Auto-generated catch block
  70. e.printStackTrace();
  71. } catch (IOException e) {
  72. // TODO Auto-generated catch block
  73. e.printStackTrace();
  74. } catch (JSONException e) {
  75. // TODO Auto-generated catch block
  76. e.printStackTrace();
  77. }
  78. return null;
  79. }
  80. }
最佳答案
除了Ladlestein评论中的所有可能的解决方案之外,还有一个简单的答案就是将所有这些解压缩到AsyncTask中.

猜你在找的Android相关文章