maven_4_接口和json

2018-01-22  本文已影响55人  果芽软件

接口

物流接口

快递接口:

http://www.kuaidi100.com/query?type=快递公司代号&postid=快递单号

ps:快递公司编码:申通=”shentong” EMS=”ems” 顺丰=”shunfeng” 圆通=”yuantong” 中通=”zhongtong” 韵达=”yunda” 天天=”tiantian” 汇通=”huitongkuaidi” 全峰=”quanfengkuaidi” 德邦=”debangwuliu” 宅急送=”zhaijisong”

httpClient

package com.guoyasoft;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class HttpClient {
    public static String callHttpByStream(int timeOut, String urlLink,
            String xml, String encode) throws Exception {
        HttpURLConnection httpurlconnection = null;
        try {
            URL url = null;
            url = new URL(urlLink);
            httpurlconnection = (HttpURLConnection) url.openConnection();
            httpurlconnection.setRequestProperty("Content-type", "text/xml");
            httpurlconnection.setDoOutput(true);
            httpurlconnection.setDoInput(true);
            httpurlconnection.setRequestMethod("POST");
            httpurlconnection.setConnectTimeout(timeOut);
            httpurlconnection.setReadTimeout(timeOut);
            httpurlconnection.connect();
        } catch (Exception e) {
            throw e;
        }
        try {
            String SendData = xml;
            httpurlconnection.getOutputStream()
                    .write(SendData.getBytes(encode));
            httpurlconnection.getOutputStream().flush();
            httpurlconnection.getOutputStream().close();
        } catch (Exception e) {
            e.getStackTrace();
            throw e;
        }
        try {
            String result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    httpurlconnection.getInputStream(), encode));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            in.close();
            return result;
        } catch (Exception e) {
            e.getStackTrace();
            throw e;
        } finally {
            if (httpurlconnection != null)
                httpurlconnection.disconnect();
        }
    }

    public static void main(String[] args) {
        try {
            String httpRequest = "http://www.kuaidi100.com/query?type=shentong&postid=10023232";
            String response = HttpClient.callHttpByStream(3000,
                    "http://www.kuaidi100.com/query?type=shentong&postid=10023232", httpRequest,
                    "UTF-8");
            System.out.println("响应报文:" + response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.7</version>
        </dependency>
上一篇 下一篇

猜你喜欢

热点阅读