1. 程式人生 > >java URLconnection post傳入json 呼叫介面 以及get

java URLconnection post傳入json 呼叫介面 以及get

一、post方式呼叫介面 返回json陣列

    public static JSONObject sendPost(List<Map<String, String>> param) {
        String Json = JSONObject.toJSONString(param);
        String result = "";
        String line = "";
        BufferedReader reader = null;
        String urlPath="http://11.1.251.172:8000/flowdata/truck/";
        try {
            URL url = new URL(urlPath);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Charset", "UTF-8");
            conn.setRequestProperty("Content-Type","application/json; charset=UTF-8");
            conn.setRequestProperty("accept","application/json");
            if (Json != null && !TextUtils.isEmpty(Json)) {
                byte[] writebytes = Json.getBytes();
                conn.setRequestProperty("Content-Length", String.valueOf(writebytes.length));
                OutputStream outwritestream = conn.getOutputStream();
                outwritestream.write(Json.getBytes());
                outwritestream.flush();
                outwritestream.close();
                conn.getResponseCode();
            }
            if (conn.getResponseCode() == 200) {
                reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line = reader.readLine()) != null) {
                    result += line;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return JSONObject.parseObject(result);
    }

二、get方式呼叫介面 返回json陣列

    public static  JSONObject sendGet(List<String> list) {
        String result = "";
//將傳入的引數拼接成想要的格式
        String param = param(list);
        BufferedReader in = null;
        String url=ConfigUtils.get("checkUrl");
        try {
            String urlNameString = url + param;
            URL realUrl = new URL(urlNameString);
            System.out.println(realUrl);
            URLConnection connection = realUrl.openConnection();
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.connect();
            Map<String, List<String>> map = connection.getHeaderFields();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return JSONObject.parseObject(result);
    }


//將傳入的list引數拼接成url後面需要拼接的格式 引數以逗號分隔
    public static String param(List<String> list) {
        if (list == null){
            return  null;
        }
        StringBuilder sb = new StringBuilder();
        if (list != null && list.size() > 0) {
            for (int i = 0, len = list.size(); i < len; i++) {
                sb.append(list.get(i));
                if (i < len - 1) {
                    sb.append(",");
                }
            }
        }
        return sb.toString();
    }

 

三、呼叫介面後使用返回的json的辦法

List<Map<String, String>> paramList = Lists.newArrayList();
//然後在paramList中放入引數
JSONObject jsonObject = sendPost(paramList);
JSONArray array = jsonObject.getJSONArray("result");
for(int j = 0; j < array.size(); j++) {
JSONObject object = array.getJSONObject(j);
String op131TImeLast = object.getString("op_131_time_last");
}