Android 发送普通的post请求的方法

来源:互联网 发布:top域名好吗 编辑:程序博客网 时间:2024/06/03 19:01
public static String sendRequest(Map<String,Object> params,String url){String res=null;HttpEntity entity=null;HttpClient client=new DefaultHttpClient();HttpPost post=new HttpPost(url);List<BasicNameValuePair> list=new ArrayList<BasicNameValuePair>();Set<Map.Entry<String,Object>> entrySet=params.entrySet();for(Map.Entry<String, Object> entry:entrySet){Log.i("test",entry.getKey()+"--"+entry.getValue());BasicNameValuePair bnv=new BasicNameValuePair(entry.getKey(),String.valueOf(entry.getValue()));list.add(bnv);}try {post.setEntity(new UrlEncodedFormEntity(list));Log.i("test","before send request");HttpResponse response = client.execute(post);Log.i("test","after send request");Log.i("test",response.getStatusLine().getStatusCode()+"");if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){entity=response.getEntity();res=EntityUtils.toString(entity);}} catch (ClientProtocolException e) {Log.i("test",e.getMessage());} catch (IOException e) {Log.i("test",e.getMessage());}finally{try {if(entity!=null){entity.consumeContent();}client.getConnectionManager().shutdown();} catch (IOException e) {e.printStackTrace();}}return res;}

原创粉丝点击