快递100官方给出的java 版示例API无法使用,返回结果全是乱码,只能自己写一个。
采用httpClient,不采用官方给出的URL方式。返回结果编码方式为UTF-8。
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
/**
* kuaidi100 java版API使用
* @author backkom
* @param
*/
public class Uod {
public static void main(String[] args) {
String url = "http://api.kuaidi100.com/api?id=***&com=debangwuliu&nu=111111&show=2&muti=1&order=asc";
String result = null;
try {
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(url);
httpClient.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
getMethod.getParams().setParameter("http.method.retry-handler",
new DefaultHttpMethodRetryHandler());
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode == 200) {
StringBuffer temp = new StringBuffer();
InputStream in = getMethod.getResponseBodyAsStream();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(in, "UTF-8"));
for (String tempstr = ""; (tempstr = buffer.readLine()) != null;)
temp = temp.append(tempstr);
buffer.close();
in.close();
result = temp.toString().trim();
System.out.println(result);
} else {
System.err.println((new StringBuilder("Can't get page:"))
.append(url).append("#").append(
getMethod.getStatusLine()).toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
转载请注册出处【http://bakcom.iteye.com/】。有此问题的朋友,直接使用即可,等官方给出正确的示例API,黄花菜都凉了...
手机扫一扫,欢迎关注公众号
关注程序员成长