android如何获取网页数据
android如何获取网页数据
1、首先在AndroidManifest中加入Internet权限: 1.<!-- 访问网络的权限 --> 2. <uses-permission Android:name="android.permission.INTERNET"/> Activity中得代码如下 1.public class GetHtmlCodeActivity extends Activity { 2. @Override 3. public void onCreate(Bundle savedInstanceState) { 4. super.onCreate(savedInstanceState); 5. setContentView(R.layout.main); 6. 7. TextView textView = (TextView)this.findViewById(R.id.picture_textview); 8. try { 9. textView.setText(getPictureData("http://www.facebook4covers.com")); 10. } catch (Exception e) { 11. Log.e("GetHtmlCodeActivity", e.toString()); 12. Toast.makeText(GetHtmlCodeActivity.this, "网络连接失败", 1).show(); 13. } 14. } 15. //得到图片的二进制数据 16. public String getPictureData(String path) throws Exception{ 17. // 类 URL 代表一个统一资源定位符,它是指向互联网“资源”的指针。 18. URL url = new URL("http://www.lewfree.com/"); 19. // 每个 HttpURLConnection 实例都可用于生成单个请求, 20. //但是其他实例可以透明地共享连接到 HTTP 服务器的基础网络 21. HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 22. //设置 URL 请求的方法 23. conn.setRequestMethod("GET"); 24. //设置一个指定的超时值(以毫秒为单位), 25. //该值将在打开到此 URLConnection 引用的资源的通信链接时使用。 26. conn.setConnectTimeout(5 * 1000); 27. // conn.getInputStream()返回从此打开的连接读取的输入流 28. InputStream inStream = conn.getInputStream();// 通过输入流获取html数据 29. byte[] data = readInputStream(inStream);// 得到html的二进制数据 30. String html = new String(data); 31. return html; 32. 33. } 34. //读取输入流中的数据,返回字节数组byte[] 35. public byte[] readInputStream(InputStream inStream) throws Exception{ 36. //此类实现了一个输出流,其中的数据被写入一个 byte 数组 37. ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 38. // 字节数组 39. byte[] buffer = new byte[1024]; 40. int len = 0; 41. //从输入流中读取一定数量的字节,并将其存储在缓冲区数组buffer 中 42. while ((len = inStream.read(buffer)) != -1) { 43. // 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流 44. outStream.write(buffer, 0, len); 45. } 46. inStream.close(); 47. //toByteArray()创建一个新分配的 byte 数组。 48. return outStream.toByteArray(); 49. } 50.}
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.}