HttpClient请求网络数据的Post请求
Posted 巫山码农
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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请求的主要内容,如果未能解决你的问题,请参考以下文章
HttpClient学习--HttpClient的POST请求过程源码解读