在网上查了很多资料,感觉没有头绪。在拼接发送报文的方法有很多种
HttpClient httpclient = new HttpClient();
GetMethod gm = new GetMethod(serviceUrl);//实例化get方法,并传递要请求的url。如果不传入的话,会报空指针错误
//set the token in the header
gm.setRequestHeader("Authorization", "Bearer "+accessToken);//设置发送请求的报头。
//set the SOQL as a query param
NameValuePair[] params = new NameValuePair[1];
/**
* other option instead of query string, pass just the fields you want back:
* https://instance_name.salesforce.com/services/data/v20.0/sobjects/Account/
* 001D000000INjVe?fields=AccountNumber,BillingPostalCode
*/
params[0] = new NameValuePair("q","SELECT name, title FROM Contact LIMIT 100");
gm.setQueryString(params); //以上代码就是拼接一个query串,例如 ?a = a&b = b 这部分代码有很多实现方式。一下会慢慢总结。
httpclient.executeMethod(gm);//执行方法
String responseBody = gm.getResponseBodyAsString();//获取相应的结果,大部分为json串
//解析json字符串