android如何获取网页数据?思路正确马上结贴不纠结。
android如何获取网页数据?思路正确马上结贴不纠结。
[解决办法]
参考如下代码:
01.//第一种
02./**获取参数(ArrayList<NameValuePair> nameValuePairs,String url)后post给远程服务器
03. * 将获得的返回结果(String)返回给调用者
04. * 本函数适用于查询数量较少的时候
05. * Chen.Zhidong
06. * 2011-02-15*/
07.public String posturl(ArrayList<NameValuePair> nameValuePairs,String url){
08. String result = "";
09. String tmp= "";
10. InputStream is = null;
11. try{
12. HttpClient httpclient = new DefaultHttpClient();
13. HttpPost httppost = new HttpPost(url);
14. httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
15. HttpResponse response = httpclient.execute(httppost);
16. HttpEntity entity = response.getEntity();
17. is = entity.getContent();
18. }catch(Exception e){
19. return "Fail to establish http connection!";
20. }
21.
22. try{
23. BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
24. StringBuilder sb = new StringBuilder();
25. String line = null;
26. while ((line = reader.readLine()) != null) {
27. sb.append(line + "\n");
28. }
29. is.close();
30.
31. tmp=sb.toString();
32. }catch(Exception e){
33. return "Fail to convert net stream!";
34. }
35.
36. try{
37. JSONArray jArray = new JSONArray(tmp);
38. for(int i=0;i<jArray.length();i++){
39. JSONObject json_data = jArray.getJSONObject(i);
40. Iterator<?> keys=json_data.keys();
41. while(keys.hasNext()){
42. result += json_data.getString(keys.next().toString());
43. }
44. }
45. }catch(JSONException e){
46. return "The URL you post is wrong!";
47. }
48.
49. return result;
50.}
51.
52.//第二种
53./**获取参数指定的网页代码,将其返回给调用者,由调用者对其解析
54. * 返回String
55. * Chen.Zhidong
56. * 2011-02-15*/
57.public String posturl(String url){
58. InputStream is = null;
59. String result = "";
60.
61. try{
62. HttpClient httpclient = new DefaultHttpClient();
63. HttpPost httppost = new HttpPost(url);
64. HttpResponse response = httpclient.execute(httppost);
65. HttpEntity entity = response.getEntity();
66. is = entity.getContent();
67. }catch(Exception e){
68. return "Fail to establish http connection!"+e.toString();
69. }
70.
71. try{
72. BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
73. StringBuilder sb = new StringBuilder();
74. String line = null;
75. while ((line = reader.readLine()) != null) {
76. sb.append(line + "\n");
77. }
78. is.close();
79.
80. result=sb.toString();
81. }catch(Exception e){
82. return "Fail to convert net stream!";
83. }
84.
85. return result;
86.}
87.
88.//第三种
89./**获取指定地址的网页数据
90. * 返回数据流
91. * Chen.Zhidong
92. * 2011-02-18*/
93.public InputStream streampost(String remote_addr){
94. URL infoUrl = null;
95. InputStream inStream = null;
96. try {
97. infoUrl = new URL(remote_addr);
98. URLConnection connection = infoUrl.openConnection();
99. HttpURLConnection httpConnection = (HttpURLConnection)connection;
100. int responseCode = httpConnection.getResponseCode();
101. if(responseCode == HttpURLConnection.HTTP_OK){
102. inStream = httpConnection.getInputStream();
103. }
104. } catch (MalformedURLException e) {
105. // TODO Auto-generated catch block
106. e.printStackTrace();
107. } catch (IOException e) {
108. // TODO Auto-generated catch block
109. e.printStackTrace();
110. }
111. return inStream;
112.}