使用Get方法提交:

    其他步骤与上一节的操作相符,只是在传送地址的时候发送参数的格式如下:

    //Sname和Sage是实际的数据    name和age则是例如是输入框中的名字

    url = "服务器的地址"+ "?" + "name=" + Sname + "&age=" + Sage;

使用Post方法提交: 
    //使用NameValuePair类来保存键值对,使用NameValuePair类是因为下面需要的那个类的参数要求

    NameValuePair NameValuePair1  = new NameValuePair("name",name);

    NameValuePair NameValuePair2  = new NameValuePair("age",age);

    //使用List<NameValuePair>把两个NameValuePair对象添加进去,使用List是因为下面需要的那个类的参数要求

    List<NameValuePair> list = new ArrayList<NameValuePair>();
    list.add(NameValuePair1); 
    list.add(NameValuePair2);  

    //HttpEntity既可以看作是请求头也可以看作是响应头 , 此类用在HttpPost是非常有效
    HttpEntity httpEntity = new UrlEncodedFormEntity(list);

    //生成HttpPost对象

    HttpPost httpPost = new HttpPost(传入服务器地址不需要传入需要的参数);

    //把参数放进去

    httpPost.setEntity(httpEntity);

      //后面的再次使用HttpClient,详细参见前一节

相关文章:

  • 2021-08-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-26
  • 2021-07-25
猜你喜欢
  • 2022-02-24
  • 2021-10-26
  • 2021-10-19
  • 2021-08-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案