package com.tenwa.lease.util; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import com.amarsoft.app.awe.config.InitSecondHandCarConfig; public class VehicleAppraisementUtil { 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; } }