客户端向服务器提交数据,表单形式

     客户端也可以像web页面一样,以表单的形式向服务器提交数据。服务器做同样的处理。

     方法:

        写一个方法,把要提交的内容作为参数封装。

         如果内容比较少,直接写在URL后面

         要是内容比较多,用List集合来装发送的参数,在发送参数过程中UrlEncodedFormEntity方法,来进行字符编码的转换和数据的发送。

         注意:被封装的参数必须是nameValuePair类型,在开发中用BasicValuepair来封装要提价的参数。

   以URL发送的

   //通过用户名和密码进行服务器端的查询,发送get请求,并获得响应结果。
    private String query(String username,String password){
        //拼凑查询字符串。
        String str="username="+username+"&password="+password;
        //查询url
        String url=HttpsUtil.BASE_URL+"/LoginServlet?"+str;
        //查询并返回结果
        return HttpsUtil.queryStringGet(url);
    }

以集合方式发送

  

 public UrlEncodedFormEntity doEntity() throws UnsupportedEncodingException{
        //获取前台信息
        String idStr = id.getText().toString();
        String nameStr = name.getText().toString();
        String pwdStr = pwd.getText().toString();
        //创建集合,用于封装数据
        List<NameValuePair> p = new ArrayList<NameValuePair>();
        //添加要封装的数据
        p.add(new BasicNameValuePair("name", nameStr));
        p.add(new BasicNameValuePair("id", idStr));
        p.add(new BasicNameValuePair("pwd", pwdStr));
        
        
        return new UrlEncodedFormEntity(p, HTTP.UTF_8);
        
    }//doEntity
原文地址:https://www.cnblogs.com/LuckStarShine/p/2705294.html