分享

Android通过URL获取网络数据(2)

 black library 2011-11-08

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

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

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多