首页 > Web开发 > 详细

HttpClient请求网络数据的Post请求

时间:2016-08-26 12:19:33      阅读:251      评论:0      收藏:0      [点我收藏+]

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请求

原文:http://www.cnblogs.com/wushanmanong/p/5809650.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!