快递100快递java_快递100 java 示例API 返回结果乱码 之替代方案

快递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,黄花菜都凉了...

手机扫一扫,欢迎关注公众号

关注程序员成长

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值