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");
}

猜你喜欢

转载自blog.csdn.net/weixin_42201566/article/details/82379887