Android Post方法请求URL 迭代器

来源:互联网 发布:淘宝罗家的衣服怎么样 编辑:程序博客网 时间:2024/06/11 22:13
  1. protected static CommResult HttpPost(Context context, String url, 
  2.             HashMap<String, String> map) { 
  3.         synchronized ("http post") { 
  4.             CommResult result = new CommResult(); 
  5.  
  6.             HttpClient httpClient = getNewHttpClient(context); 
  7.  
  8.             HttpPost httpPost = new HttpPost(url); 
  9.  
  10.             ArrayList<BasicNameValuePair> postDate = new ArrayList<BasicNameValuePair>(); 
  11.  
  12.             Set<String> set = map.keySet(); 
  13.  
  14.             Iterator<String> iterator = set.iterator(); 
  15.  
  16.             while (iterator.hasNext()) { 
  17.                 String key = (String) iterator.next(); 
  18.                 postDate.add(new BasicNameValuePair(key, map.get(key))); 
  19.  
  20.             } 
  21.             try
  22.                 UrlEncodedFormEntity entity = new UrlEncodedFormEntity( 
  23.                         postDate, HTTP.UTF_8); 
  24.                 httpPost.setEntity(entity); 
  25.                 HttpResponse response = httpClient.execute(httpPost); 
  26.  
  27.                 InputStream in = response.getEntity().getContent(); 
  28.                 int statusCode = response.getStatusLine().getStatusCode(); 
  29.                 String message = InputStreamToString(in); 
  30.  
  31.                 result.setMessage(message); 
  32.                 result.setResponseCode(String.valueOf(statusCode)); 
  33.  
  34.             } catch (UnsupportedEncodingException e) { 
  35.                 e.printStackTrace(); 
  36.             } catch (ClientProtocolException e) { 
  37.                 e.printStackTrace(); 
  38.             } catch (IOException e) { 
  39.                 e.printStackTrace(); 
  40.             } 
  41.  
  42.             return result; 
  43.         } 
  44.     }