从网络获取数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从网络获取数据相关的知识,希望对你有一定的参考价值。

  1. HttpClient httpClient = new DefaultHttpClient();
  2.  
  3. HttpGet httpPost = new HttpGet("http://www.facebookwidgets4all.com/clients/thumbsupchamp/thumbsupview.php?top=5");
  4. httpPost.addHeader("Content-Type","application/x-www-form-urlencoded");
  5.  
  6. HttpResponse httpResponse;
  7. try {
  8. httpResponse = httpClient.execute(httpPost);
  9. HttpEntity httpEntity = httpResponse.getEntity();
  10.  
  11. if (httpEntity !=null) {
  12. InputStream inputStream = httpEntity.getContent();
  13.  
  14. // Convert Stream to String
  15. result = convertStreamToString(inputStream);
  16. inputStream.close();
  17. httpClient =null;
  18. httpPost.abort();
  19. }
  20.  
  21. } catch (ClientProtocolException e) {
  22. e.printStackTrace();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. }
  28. int count = 0;
  29. ArrayList<String> arrList = new ArrayList<String>();
  30. String str = result;
  31. int index = 0;
  32. String str1 = null;
  33.  
  34. while(str.contains("<br />")){
  35.  
  36. str1 = str.substring(0, str.indexOf("<br />"));
  37. str = str.substring(str.indexOf("<br />")+"<br />".length());
  38.  
  39. arrList.add(str1);
  40. count++;
  41. }
  42. _dataList = new ArrayList<Data>();
  43. for(int i = 0;i < arrList.size();i++){
  44.  
  45. Data _data = new Data();
  46. st = new StringTokenizer(arrList.get(i),",");
  47. while(st.hasMoreTokens()){
  48.  
  49. _data._initial = st.nextToken().replace('"',' ');
  50. _data._email = st.nextToken().replace('"',' ');
  51. _data._smartPhone = st.nextToken().replace('"',' ');
  52. _data._country = st.nextToken().replace('"',' ');
  53. _data._wpm = st.nextToken().replace('"',' ');
  54.  
  55. Log.i("array ", ""+ _data._initial + " " + _data._email + " " + _data._smartPhone);
  56. _dataList.add(_data);
  57. }
  58. }

以上是关于从网络获取数据的主要内容,如果未能解决你的问题,请参考以下文章

从网络获取数据时如何隐藏和显示进度条?

从网络获取数据

从多个社交网络(Facebook 和 Linkedin...)获取统一配置文件的数据库方案

tcptump的使用------使用JAVA与tcpdump从网络获取原始数据

Wio Terminal 从网络获取天气数据

Wio Terminal 从网络获取天气数据