【android初级】之Android获取网页数据的方法总结

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


//第一种
/**获取参数(ArrayList<NameValuePair> nameValuePairs,String url)后post给远程服务器
  * 将获得的返回结果(String)返回给调用者
  * 本函数适用于查询数量较少的时候
*/
public String posturl(ArrayList<NameValuePair> nameValuePairs,String url){
     String result = "" ;
     String tmp= "" ;
     InputStream is = null ;
     try {
         HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost(url);
         httppost.setEntity( new UrlEncodedFormEntity(nameValuePairs));
         HttpResponse response = httpclient.execute(httppost);
         HttpEntity entity = response.getEntity();
         is = entity.getContent();
     } catch (Exception e){
         return "Fail to establish http connection!" ;
     }
 
     try {
         BufferedReader reader = new BufferedReader( new InputStreamReader(is, "utf-8" ));
         StringBuilder sb = new StringBuilder();
         String line = null ;
         while ((line = reader.readLine()) != null ) {
             sb.append(line + "\n" );
         }
         is.close();
 
         tmp=sb.toString();
     } catch (Exception e){
         return "Fail to convert net stream!" ;
     }
 
     try {
         JSONArray jArray = new JSONArray(tmp);
         for ( int i= 0 ;i<jArray.length();i++){
             JSONObject json_data = jArray.getJSONObject(i);
             Iterator<?> keys=json_data.keys();
             while (keys.hasNext()){
                 result += json_data.getString(keys.next().toString());
             }
         }
     } catch (JSONException e){
         return "The URL you post is wrong!" ;
     }
 
     return result;
}
 
//第二种
/**获取参数指定的网页代码,将其返回给调用者,由调用者对其解析
  * 返回String
*/
public String posturl(String url){
     InputStream is = null ;
     String result = "" ;
 
     try {
         HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost(url);
         HttpResponse response = httpclient.execute(httppost);
         HttpEntity entity = response.getEntity();
         is = entity.getContent();
     } catch (Exception e){
         return "Fail to establish http connection!" +e.toString();
     }
 
     try {
         BufferedReader reader = new BufferedReader( new InputStreamReader(is, "utf-8" ));
         StringBuilder sb = new StringBuilder();
         String line = null ;
         while ((line = reader.readLine()) != null ) {
             sb.append(line + "\n" );
         }
         is.close();
 
         result=sb.toString();
     } catch (Exception e){
         return "Fail to convert net stream!" ;
     }
 
     return result;
}
 
//第三种
/**获取指定地址的网页数据
  * 返回数据流
*/
public InputStream streampost(String remote_addr){
     URL infoUrl = null ;
     InputStream inStream = null ;
     try {
         infoUrl = new URL(remote_addr);
         URLConnection connection = infoUrl.openConnection();
         HttpURLConnection httpConnection = (HttpURLConnection)connection;
         int responseCode = httpConnection.getResponseCode();
         if (responseCode == HttpURLConnection.HTTP_OK){
             inStream = httpConnection.getInputStream();
         }
     } catch (MalformedURLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
     return inStream;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值