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

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

关注程序员成长

成长的乐趣,在于分享!
大龄程序员,一路走来,感慨颇多。闲暇时写写字,希望能给同行人一点帮助。
本文版权归作者growithus和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/growithus/p/11012287.html