您的位置:首页 > 理论基础 > 计算机网络

HttpClient--BasicNameValuePair

2016-07-10 22:42 686 查看
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.rpc.ServiceException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONObject;

public class Test3 {
public static void main(String[] args) throws ServiceException, ClientProtocolException, IOException {
HttpClient hc=HttpClients.createDefault();
HttpPost post=new HttpPost("http://62.xx.xx.122:9xx0/TaxHttpService/tax_getInfo");
Map map=new HashMap();
map.put("infoKind",2);
String params=JSONObject.fromObject(map).toString();

List<BasicNameValuePair> param=new ArrayList<BasicNameValuePair>();
param.add(new BasicNameValuePair("inputJson", params));
//HttpEntity hh=new StringEntity(params,"UTF-8");
UrlEncodedFormEntity he=new UrlEncodedFormEntity(param,"UTF-8");
post.setEntity(he);
HttpResponse res=hc.execute(post);
HttpEntity entity=res.getEntity();
String msg=EntityUtils.toString(entity,"UTF-8");
System.out.println(msg);
}
}

传递简单{'a':{'c':'d'}}形式的json数据,只能用上面的转换来转换去才成功调通,

这里要记一下EntityUtils和UrlEncodedFormEntity类,一个转换HttpEntity为字符串,一个是对HttpEntity先转换成UTF-8,然后再用URLEncoder转码

下面构造的数据不能用Map存储键值再转换成JSON,暂时并不知道为什么,可能是对方接口解析接收参数的问题,所以弄得很古灵精怪的,用下面的方法才接通。

构造的数据是:

[inputJson={"infoKind":2,"cName":"fsdfs","taxRate":17,"invoicer":"zzzzz","sAddress":"ddddddddddd","sBank":"ddddddddddd","details":[{"amount":88.12,"goodsName":"asdaas","priceKind":0,"taxRate":17}]}]

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Test4 {

public static void main(String[] args) throws ClientProtocolException, IOException {
// TODO Auto-generated method stub
new Test4().test();
}
public String invoice(){
JSONObject obj=new JSONObject();
obj.put("infoKind", 2);
obj.put("cName", "fsdfs");
obj.put("taxRate", 17);
obj.put("invoicer", "zzzzz");
obj.put("sAddress", "ddddddddddd");
obj.put("sBank", "ddddddddddd");
////////////////////////////////
JSONArray arr=new JSONArray();
JSONObject d=new JSONObject();
d.put("amount", 88.12);
d.put("goodsName", "asdaas");
d.put("priceKind", 0);
d.put("taxRate", 17);
 d.put("goodsTaxNo", "10101013301");
    d.put("taxPre", 0);
    d.put("taxPreCon", "免税");
    d.put("goodsNoVer", "1.0");
 ////////////////////////////////
//String jsonstr=d.toString();
arr.add(d);
obj.put("details", arr);
System.out.println(obj.toString());
return obj.toString();
}
public void test() throws ClientProtocolException, IOException{
HttpClient hc=HttpClients.createDefault();
HttpPost post=new HttpPost("http://11.11.11.11xxxx");
List<BasicNameValuePair> nv=new ArrayList<BasicNameValuePair>();
nv.add(new BasicNameValuePair("inputJson", invoice()));
UrlEncodedFormEntity he=new UrlEncodedFormEntity(nv,"utf-8");
post.setEntity(he);
HttpResponse res=hc.execute(post);
HttpEntity entity=res.getEntity();
String msg=EntityUtils.toString(entity,"UTF-8");
System.out.println(">>>>>>>>>>>>>>>>>"+msg);
}
}


总结:对接别人的接口时,切记要问清楚传递参数的格式,这里对方的参数格式是inputJson={'a':'c'}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HttpClient