首先是调用get请求,以查询高德地图轨迹数据为例:
private static final String SEARCH_URL= "https://tsapi.amap.com/v1/track/terminal/trsearch"; // 轨迹查询url
private static final String DELETE_URL = "https://tsapi.amap.com/v1/track/trace/delete"; // 删除轨迹url
private static final String SERVICE_KEY = "xxx"; // 请求服务权限标识
private static final String SERVICE_SID = "xxx"; // 服务的唯一编号
String tid = getPara("tid");
String trid = getPara("trid");
StringBuffer url = new StringBuffer();
url.append(SEARCH_URL)
.append("?key=").append(SERVICE_KEY)
.append("&sid=").append(SERVICE_SID)
.append("&tid=").append(tid)
.append("&trid=").append(trid);
HttpKit.get(url.toString());
调用get请求时只需要把参数拼接在url后面即可。
然后是post请求,对于post请求框架自带的HttpKit.post(url, data)能应对大部分问题,但仍有一部分情况是不行的,以高德地图删除轨迹为例
Map<String, String> map = new HashMap<String, String>();
map.put("key",SERVICE_KEY);
map.put("sid", SERVICE_SID);
map.put("tid", tid);
map.put("trid", trid);
HttpKit.post(DELETE_URL , JSON.toJSONString(map));
测试结果会提示参数出错,但以相同的参数调用查询轨迹是能够查到数据的,也就是说这种方式是存在问题的,用postman对删除轨迹的接口进行测试,发现参数以x-www-form-urlencoded方式传输,也是能测试通过的,再以上述方式进行调试,能看到有conn.setRequestMethod(method);和conn.setRequestProperty(“Content-Type”,“application/x-www-form-urlencoded”);的设置,也就是设置了请求头和请求类型,但是在查看HttpURLConnection对象时却发现这些值没有设置进去,请求还是get请求(默认为get),request还是为空,挣扎了一番之后还是放弃了,另寻出路,于是搜到如下方式
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.impl.client.DefaultHttpClient;
@SuppressWarnings("deprecation")
public class SSLClient extends DefaultHttpClient {
public SSLClient() throws Exception{
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientUtil {
@SuppressWarnings({ "unchecked", "rawtypes" })
public String doPost(String url,Map<String,String> map,String charset){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = (HttpClient) new SSLClient();
httpPost = new HttpPost(url);
//设置参数
List<NameValuePair> list = new ArrayList<NameValuePair>();
Iterator iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Entry<String,String> elem = (Entry<String, String>) iterator.next();
list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
}
if(list.size() > 0){
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
httpPost.setEntity(entity);
}
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
}
虽然这些看不太懂,但是能解决当前问题,只要直接调用就好了,还是很方便的
Map<String, String> map = new HashMap<String, String>();
map.put("key", SERVICE_KEY);
map.put("sid", SERVICE_SID);
map.put("tid", tid);
map.put("trid", trid);
HttpClientUtil util = new HttpClientUtil();
util.doPost(DELETE_URL, map, "utf-8");