HTTPClient PostMethod

PostMethod 发送post请求,示例代码:

 方法一:

 1 private void sendMessage() {
 2 
 3         try {
 4 
 5             HttpClient client = new HttpClient();
 6             PostMethod postMethod = new PostMethod(send_url);   //需要导入commons-httpclient-3.1.jar(os-china或jzlfxxt的libs中都有)
 7             postMethod.getParams().setParameter(
 8                     HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");//消除乱码问题
 9             postMethod.addParameter("phoneName", phoneName);
10             postMethod.addParameter("network_type", network_type);
11             postMethod.addParameter("phone_type", phone_type);
12             postMethod.addParameter("imei", imei);
13             postMethod.addParameter("imeisv", imeisv);
14             postMethod.addParameter("mac", mac);
15             postMethod.addParameter("serial", serial);
16             postMethod.addParameter("androidSDK", androidSDK);
17             int statCode = client.executeMethod(postMethod);// 试试不要
18             SharedPreferences preferences = context.getSharedPreferences(
19                     "LoginMessage", Context.MODE_PRIVATE);
20             Editor edit = preferences.edit();
21 
22             if (statCode == HttpStatus.SC_OK) {
23                 edit.putBoolean("isSendPM", false);
24             } else {
25                 edit.putBoolean("isSendPM", true);
26             }
27             edit.commit();
28 
29         } catch (Exception e) {
30             e.printStackTrace();
31         }
32     }

 方法二:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(getResources()
.getString(R.string.login_url));
JSONObject jsonObject = new JSONObject();
jsonObject.put("userAccount", name);
jsonObject.put("userPassword", password);
StringEntity stringEntity = new StringEntity(jsonObject.toString(), "utf-8");
post.setEntity(stringEntity);

HttpResponse httpResponse =     client.execute(post);//接收返回回来的信息

 

 

 

 

posted @ 2014-02-26 14:22  寡蛋  阅读(1565)  评论(0)    收藏  举报