快递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(); } } }
手机扫一扫,欢迎关注公众号
关注程序员成长
相关推荐
染血白衣 2020-11-16
SAMXIE 2020-11-04
一个智障 2020-11-15
学习web前端 2020-11-09
yiranpiaoluo 2020-11-04
lxhuang 2020-11-03
88274956 2020-11-03
82387067 2020-11-03
huangliuyu00 2020-10-29
sichenglain 2020-10-27
Dayer 2020-10-27
小马的学习笔记 2020-10-23
liuweiITlove 2020-10-14
kjyiyi 2020-10-10
fanjunjaden 2020-10-09
zhyue 2020-09-28
huangliuyu00 2020-09-24
88397813 2020-09-23
jyj0 2020-09-21