package com.tenwa.lease.util; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.apache.commons.jexl2.Main; import com.amarsoft.app.awe.config.InitSecondHandCarConfig; public class VehicleAppraisementUtil { public static void main(String[] args) { // 二手车prefix,测试http://testapi.che300.com;生产http://api.che300.com String prefix = "http://testapi.che300.com"; // 二手车token,测试a6b83424b55410c36415178002f8415e;生产0685c8275534d8d391cdaa5a5cf48c73 String token = "a6b83424b55410c36415178002f8415e"; // 二收车参数regDate上牌日期 String regDate = "2017-12-02"; // 二收车参数里程数(万公里) String mile = "10.00"; // 二收车评估城市区划代码110100北京 String zone = "110100"; String urlStr = prefix + "/service/getUsedCarPrice?token=" + token + "&modelId=1128683®Date=" + regDate + "&mile=" + mile + "&zone=" + zone; String result = VehicleAppraisementUtil.getResponseResult(urlStr); System.out.println(result); } public static String getVehicleAppraisementResponse(String modelId, String zone, String regDate, String mile) { String urlStr = InitSecondHandCarConfig.PREFIX + "/service/getUsedCarPrice?" + "token=" + InitSecondHandCarConfig.TOKEN + "&modelId=" + modelId + "®Date=" + regDate + "&mile=" + mile + "&zone=" + zone + ""; String result = VehicleAppraisementUtil.getResponseResult(urlStr); return result; } // 通过请求url返回Json数据 public static String getResponseResult(String url) { String line = ""; String httpResponseResult = ""; try { HttpURLConnection connection = VehicleAppraisementUtil.getURLConnection(url); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); connection.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); while ((line = reader.readLine()) != null) { httpResponseResult += line.toString(); } } catch (Exception e) { e.printStackTrace(); } return httpResponseResult; } // 获取请求需要的HttpURLConnection连接 public static HttpURLConnection getURLConnection(String url) { HttpURLConnection connection = null; try { URL newUrl = new URL(url); connection = (HttpURLConnection) newUrl.openConnection(); connection.setDoOutput(false);// post请求应改为true connection.setDoInput(true); connection.setRequestMethod("GET"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Charset", "utf-8"); connection.setRequestProperty("Accept-Charset", "utf-8"); } catch (Exception e) { e.printStackTrace(); } return connection; } }