Fetch Data From Network


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  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. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.