HttpClient Post提交数据

来源:互联网 发布:linux help和man 编辑:程序博客网 时间:2024/05/29 04:33

项目用由于需要用到post来提交数据,主要是因为自己原先用get时字符串中带有‘’+‘’号的会被转义成空的,所以只能改成post方式进行提交了,整个项目没见有post提交方式,

所以网上也找了许多,不过现在好了。

用post进行提交数据:

      String hr_token = mSP.getString(CommonData.HR_TOKEN, "");//该token为数据库进行获取      String strUrl = "http://" + CommonData.HRServerIP+ CommonData.HeartBeatCheck + "?terminalID=" + mDeviceID;//mDeviceID为设备ID也是由其它进行传进行      String strHttps = HttpSend.post(strUrl,hr_token);//这里主要是调用post进行提交token
下面为调用的post方法:
public static String post(String Url ,String token) throws Exception {String path = Url;HttpClient client = new DefaultHttpClient();HttpPost httpPost = new HttpPost(path);List<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("Token", token));httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));HttpResponse response = client.execute(httpPost);if (200 == response.getStatusLine().getStatusCode()){//return response.getEntity().getContent();}return response.getStatusLine().getStatusCode()+"";}