HttpClient请求网络数据的Post请求

本文介绍了一个基于Android的应用中实现用户注册功能的方法。通过使用HttpClient发起HTTP Post请求,并将用户输入的手机号、密码和个人姓名等信息作为参数传递到指定URL路径。文章详细展示了如何对中文用户名进行UTF-8编码及整体请求流程。

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();

转载于:https://www.cnblogs.com/changyiqiang/p/5779941.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值