首页 > 代码库 > HttpClient请求网络数据的Post请求
HttpClient请求网络数据的Post请求
new Thread(){
public void run() {
try {
//获得输入框内容
String phonenum=et_phone_num.getText().toString().trim();
String password=et_password.getText().toString().trim();
String name=et_name.getText().toString().trim();
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost(urlPath);
//将汉字编码
String ss=URLEncoder.encode(name, "utf-8");
List<NameValuePair> parameters=new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("userName", ss));
parameters.add(new BasicNameValuePair("userPhone", phonenum));
parameters.add(new BasicNameValuePair("userPassword", password));
HttpEntity entity=new UrlEncodedFormEntity(parameters,"utf-8");
post.setEntity(entity);
HttpResponse response=client.execute(post);
StatusLine line=response.getStatusLine();
int code=line.getStatusCode();
if (code==200) {
HttpEntity entity2=response.getEntity();
String result=EntityUtils.toString(entity2,"utf-8");
Message message=new Message();
message.obj=result;
message.what=1;
handler.sendMessage(message);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
HttpClient请求网络数据的Post请求