一:post传参的两种形式
两种方式,最近本的构造参数方法一致:定义了一个list,该list的数据类型是NameValuePair(简单名称值对节点类型),这个代码多处用于Java像url发送Post请求。在发送post请求时用该list来存放参 数
String url=" http://www.baidu.com ";
HttpPost httppost=new HttpPost(url); //建立HttpPost对象
List<NameValuePair> params=new ArrayList<NameValuePair>();
//建立一个NameValuePair数组,用于存储欲传送的参数
params.add(new BasicNameValuePair("pwd","2544"));
//添加参数
1.发送最基本参数。(普通的名:值,键值对)。
UrlEncodedFormEntity ue=null;
ue = new UrlEncodedFormEntity(params,"utf-8");
httpPost.setEntity(ue);
2.发送字符串形式,一般用于json格式。
JSONObject jsonObject = new JSONObject(); jsonObject.put("KEY1", "VALUE1"); jsonObject.put("KEY2", "VALUE2"); StringEntity s = new StringEntity(jsonObject.toString(), "utf-8"); httpPost.setEntity(s);
其实,采用 StringEntity 就是形式比较自由了,除了json,你也可以使用其它任意的字符串,只要服务端能做相应处理即可。
s.setContentType("application/json");
httpPost.setEntity(s);
总结:post传递参数用的就是NameValuePair的list。
//get传参
String yyl = String.format("https://w.bayd.com/get?secids=%s&d=%d", secids,ij);
httpGet=new HttpGet(yyl);
总结:get方法传参直接构造链接即可!