分享

Android获取网页数据的方法总结

 black library 2011-11-08

本文总结了三种获取网页数据的代码,是自己在用的时候随手整理出来的。此处仅贴出函数段,不贴出import了,用的时候可以用eclipse自动import一下就行了。函数的详细用途描述请看代码中注释。调用的时候请对应函数需要的参数

Java代码 复制代码 收藏代码
  1. //第一种   
  2. /**获取参数(ArrayList<NameValuePair> nameValuePairs,String url)后post给远程服务器  
  3.  * 将获得的返回结果(String)返回给调用者  
  4.  * 本函数适用于查询数量较少的时候  
  5.  * Chen.Zhidong  
  6.  * 2011-02-15*/  
  7. public String posturl(ArrayList<NameValuePair> nameValuePairs,String url){   
  8.     String result = "";   
  9.     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. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多