android客户端给服务端发post请求

来源:互联网 发布:php 函数 变量='' 编辑:程序博客网 时间:2024/06/05 02:17

public class HttpUtil {

public static String send(Map<String,String> map,String path){

  String rs = null;

      HttpPost hp = new HttpPost(path);

      DefaultHttpClient hc = new DefaultHttpClient();

      List<NameValuePair> list = new ArrayList<NameValuePair>();

      try {

        Set keys = map.entrySet();

      Iterator it = keys.iterator();  

      while (it.hasNext()) {  

        Map.Entry entry = (Map.Entry) it.next();  

        String key = (String) entry.getKey();  

        String value = (String) entry.getValue();  

    BasicNameValuePair nm = new BasicNameValuePair(key, value);

          list.add(nm);

      }  

          HttpEntity entity = new UrlEncodedFormEntity(list,HTTP.UTF_8);

          hp.setEntity(entity);

          HttpResponse response = hc.execute(hp);

 

          if (response.getStatusLine().getStatusCode() == 200) {

              entity = response.getEntity();

              rs = EntityUtils.toString(entity);

          }

 

      } catch (ClientProtocolException e) {

          e.printStackTrace();

      } catch (IOException e) {

          e.printStackTrace();

      } 

      return rs;

}

}


原创粉丝点击