点击(此处)折叠或打开

  1. package com.glebpopov.hackernews.net;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import org.apache.http.HttpEntity;
  8. import org.apache.http.HttpResponse;
  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.JSONArray;
  13. import org.json.JSONException;
  14. import org.json.JSONObject;
  15. import com.glebpopov.hackernews.domain.CommentItem;
  16. import com.glebpopov.hackernews.domain.NewsContainer;
  17. import com.glebpopov.hackernews.domain.NewsItem;
  18. import android.util.Log;
  19. public class DataParser
  20. {
  21. private String dataUrl = null;
  22. private String TAG = "DataParser";
  23. public DataParser(String url)
  24. {
  25. dataUrl = url;
  26. }
  27. public String getUrl()
  28. {
  29. return dataUrl;
  30. }
  31. //returns HTTP code
  32. public int submitData()
  33. {
  34. if (getUrl() == null || getUrl().length() == 0)
  35. {
  36. Log.e(TAG, "getJsonData: invalid URL");
  37. return 500;
  38. }
  39. // Create the httpclient
  40. HttpClient httpclient = new DefaultHttpClient();
  41. // Prepare a request object
  42. HttpGet httpget = new HttpGet(getUrl());
  43. // Execute the request
  44. HttpResponse response;
  45. try
  46. {
  47. // Open the webpage.
  48. response = httpclient.execute(httpget);
  49. return response.getStatusLine().getStatusCode();
  50. }
  51. catch (IOException ex)
  52. {
  53. // thrown by line 80 - getContent();
  54. // Connection was not established
  55. Log.e(TAG, "getJsonData: Connection failed: " + ex.getMessage());
  56. }
  57. finally
  58. {
  59. }
  60. return 500;
  61. }
  62. public NewsContainer getNews()
  63. {
  64. if (dataUrl == null || dataUrl.length() == 0)
  65. {
  66. Log.e(TAG, "getNews: invalid URL");
  67. return null;
  68. }
  69. Log.d(TAG, "URL: " + dataUrl);
  70. NewsContainer container = null;
  71. JSONArray jsonData = getJsonData();
  72. if (jsonData != null)
  73. {
  74. container = new NewsContainer();
  75. ArrayList data = new ArrayList();
  76. NewsItem item = null;
  77. NewsItem moreLink = null;
  78. //shouldn't return links to NEXT...links to next should be appended somewhere else
  79. for (int i=0; i
  80. {
  81. try
  82. {
  83. if (jsonData.getJSONObject(i).getString("title") != null &&
  84. jsonData.getJSONObject(i).getString("title").toLowerCase().equals("nextid")
  85. )
  86. {
  87. moreLink = new NewsItem();
  88. moreLink.setTitle(jsonData.getJSONObject(i).getString("title"));
  89. moreLink.setUrl("http://hndroidapi.appspot.com" + jsonData.getJSONObject(i).getString("url"));
  90. } else
  91. {
  92. int id = 0;
  93. try
  94. {
  95. id = Integer.parseInt(jsonData.getJSONObject(i).getString("item_id"));
  96. } catch (Exception ex) {}
  97. item = new NewsItem();
  98. item.setTitle(jsonData.getJSONObject(i).getString("title"));
  99. item.setUrl(jsonData.getJSONObject(i).getString("url"));
  100. item.setPostedDate(jsonData.getJSONObject(i).getString("time"));
  101. item.setPoints(jsonData.getJSONObject(i).getString("score"));
  102. item.setAuthor(jsonData.getJSONObject(i).getString("user"));
  103. item.setComments(jsonData.getJSONObject(i).getString("comments"));
  104. item.setId(id);
  105. //add to container
  106. data.add(item);
  107. }
  108. } catch (Exception ex)
  109. {
  110. Log.e(TAG, "getNews: exception while parsing JSON data: " + ex);
  111. }
  112. }
  113. container.setNewsContainer(data);
  114. container.setMoreNewsLink(moreLink);
  115. }
  116. if (container != null && container.getNewsContainer() != null)
  117. {
  118. Log.d(TAG, "getNews: returning " + container.getNewsContainer().size() + " elements");
  119. } else
  120. {
  121. Log.w(TAG, "getNews: failed to retrieve news data");
  122. }
  123. return container;
  124. }
  125. public ArrayList getComments()
  126. {
  127. if (dataUrl == null || dataUrl.length() == 0)
  128. {
  129. Log.e(TAG, "getComments: invalid URL");
  130. return null;
  131. }
  132. JSONArray jsonData = getJsonData();
  133. ArrayList data = parseComments(jsonData, -1);
  134. Log.d(TAG, "getComments: returning " + data.size() + " elements");
  135. return data;
  136. }
  137. public ArrayList parseComments(JSONArray jsonData, int parentId)
  138. {
  139. //Log.d(TAG, "parseComments: ParentID=" + parentID);
  140. ArrayList data = new ArrayList();
  141. if (jsonData != null)
  142. {
  143. //Log.d(TAG, "parseComments: Container Count=" + jsonData.length());
  144. CommentItem item = null;
  145. for (int i=0; i
  146. {
  147. item = new CommentItem();
  148. try
  149. {
  150. int id = -1;
  151. try
  152. {
  153. id = Integer.parseInt(jsonData.getJSONObject(i).getString("id"));
  154. } catch (Exception ex) {}
  155. item.setId(id);
  156. item.setParentId(parentId);
  157. item.setComment(jsonData.getJSONObject(i).getString("comment"));
  158. item.setReplyId(jsonData.getJSONObject(i).getString("reply_id"));
  159. item.setPostedDate(jsonData.getJSONObject(i).getString("time"));
  160. item.setAuthor(jsonData.getJSONObject(i).getString("username"));
  161. if (jsonData.getJSONObject(i).getJSONArray("children") != null)
  162. {
  163. item.setChildren(parseComments(jsonData.getJSONObject(i).getJSONArray("children"), id));
  164. }
  165. } catch (Exception ex)
  166. {
  167. Log.e(TAG, "parseComments: exception while parsing JSON data: " + ex);
  168. }
  169. data.add(item);
  170. }
  171. }
  172. return data;
  173. }
  174. public JSONArray getJsonData()
  175. {
  176. if (dataUrl == null || dataUrl.length() == 0)
  177. {
  178. Log.e(TAG, "getJsonData: invalid URL");
  179. return null;
  180. }
  181. // Create the httpclient
  182. HttpClient httpclient = new DefaultHttpClient();
  183. // Prepare a request object
  184. HttpGet httpget = new HttpGet(dataUrl);
  185. // Execute the request
  186. HttpResponse response;
  187. try {
  188. // return string
  189. String returnString = null;
  190. // Open the webpage.
  191. response = httpclient.execute(httpget);
  192. if(response.getStatusLine().getStatusCode() == 200){
  193. // Connection was established. Get the content.
  194. HttpEntity entity = response.getEntity();
  195. // If the response does not enclose an entity, there is no need
  196. // to worry about connection release
  197. if (entity != null) {
  198. // A Simple JSON Response Read
  199. InputStream instream = entity.getContent();
  200. returnString = convertStreamToString(instream);
  201. //Log.d(TAG, "Return String: " + returnString);
  202. // Load the requested page converted to a string into a JSONObject.
  203. JSONObject myAwway = new JSONObject(returnString);
  204. // Get the query value'
  205. //String query = myAwway.getString("query");
  206. // Make array of the suggestions
  207. JSONArray recs = myAwway.getJSONArray("items");
  208. if (recs != null)
  209. {
  210. Log.d(TAG, "getJsonData: Got JSON Data: " + recs.length());
  211. } else
  212. {
  213. Log.w(TAG, "getJsonData: no data returned");
  214. }
  215. // Cose the stream.
  216. instream.close();
  217. return recs;
  218. }
  219. }
  220. else {
  221. // code here for a response othet than 200. A response 200 means the webpage was ok
  222. // Other codes include 404 - not found, 301 - redirect etc...
  223. // Display the response line.
  224. Log.e(TAG, "getJsonData: Unable to load page. Status: " + response.getStatusLine());
  225. }
  226. }
  227. catch (IOException ex) {
  228. // thrown by line 80 - getContent();
  229. // Connection was not established
  230. Log.e(TAG, "getJsonData: Connection failed: " + ex.getMessage());
  231. }
  232. catch (JSONException ex){
  233. // JSON errors
  234. Log.e(TAG, "getJsonData: JSONException: " + ex.getMessage());
  235. } finally
  236. {
  237. }
  238. return null;
  239. }
  240. public String convertStreamToString(InputStream is) {
  241. /*
  242. * To convert the InputStream to String we use the BufferedReader.readLine()
  243. * method. We iterate until the BufferedReader return null which means
  244. * there's no more data to read. Each line will appended to a StringBuilder
  245. * and returned as String.
  246. */
  247. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  248. StringBuilder sb = new StringBuilder();
  249. String line = null;
  250. try {
  251. while ((line = reader.readLine()) != null) {
  252. sb.append(line + "\n");
  253. }
  254. } catch (IOException e) {
  255. e.printStackTrace();
  256. } finally {
  257. try {
  258. is.close();
  259. } catch (IOException e) {
  260. e.printStackTrace();
  261. }
  262. }
  263. return sb.toString();
  264. }
  265. public NewsItem getLatestNews() {
  266. Log.d(TAG, "getLatestNews");
  267. NewsContainer container = getNews();
  268. if (container != null)
  269. {
  270. ArrayList data = container.getNewsContainer();
  271. if (data != null && data.size() > 0 && data.get(0) != null && data.get(0).getTitle() != null)
  272. {
  273. Log.d(TAG, "getLatestNews: successfully retrieved " + data.size() + " records");
  274. return data.get(0);
  275. }
  276. }
  277. Log.w(TAG, "getLatestNews: no data returned");
  278. return null;
  279. }
  280. }

10-14 06:36