Android Post方法请求URL 迭代器

  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.     } 
protected static CommResult HttpPost(Context context, String url,
			HashMap<String, String> map) {
		synchronized ("http post") {
			CommResult result = new CommResult();

			HttpClient httpClient = getNewHttpClient(context);

			HttpPost httpPost = new HttpPost(url);

			ArrayList<BasicNameValuePair> postDate = new ArrayList<BasicNameValuePair>();

			Set<String> set = map.keySet();

			Iterator<String> iterator = set.iterator();

			while (iterator.hasNext()) {
				String key = (String) iterator.next();
				postDate.add(new BasicNameValuePair(key, map.get(key)));

			}
			try {
				UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
						postDate, HTTP.UTF_8);
				httpPost.setEntity(entity);
				HttpResponse response = httpClient.execute(httpPost);

				InputStream in = response.getEntity().getContent();
				int statusCode = response.getStatusLine().getStatusCode();
				String message = InputStreamToString(in);

				result.setMessage(message);
				result.setResponseCode(String.valueOf(statusCode));

			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			return result;
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值