当前位置: 代码迷 >> Web前端 >> 特快专递100 java 示例API 返回结果乱码 之替代方案
  详细解决方案

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

热度:335   发布时间:2012-12-21 12:03:49.0
快递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,黄花菜都凉了...

  相关解决方案