现在的位置: 首页 > 综合 > 正文

android用HttpClent发送请求

2018年04月19日 ⁄ 综合 ⁄ 共 825字 ⁄ 字号 评论关闭

一、权限

<uses-permission android:name="android.permission.INTERNET"/>

二、代码

//最好用到下面的情况时才使用HttpClent,功能强大,但效率不如直接用post或get请求
 //SSL HTTPS Cookie
 public static boolean sendRequestFromHttpClient(String path, Map<String, String>

params, String enc) throws Exception{
  List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
  if(params!=null && !params.isEmpty()){
   for(Map.Entry<String, String> entry : params.entrySet()){
    paramPairs.add(new BasicNameValuePair(entry.getKey(),

entry.getValue()));
   }
  }
  UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs,

enc);//得到经过编码过后的实体数据
  HttpPost post = new HttpPost(path); //form
  post.setEntity(entitydata);
  DefaultHttpClient client = new DefaultHttpClient(); //浏览器
  HttpResponse response = client.execute(post);//执行请求
  if(response.getStatusLine().getStatusCode()==200){
   return true;
  }
  return false;
 }

 

抱歉!评论已关闭.