apzl_leasing/src_tenwa/com/tenwa/lease/util/VehicleAppraisementUtil.java
2018-06-05 09:42:45 +08:00

57 lines
2.2 KiB
Java

package com.tenwa.lease.util;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class VehicleAppraisementUtil {
public static String getVehicleAppraisementResponse(String modelId,String zone,String regDate,String mile){
String urlStr = "https://api.che300.com/service/getUsedCarPrice?"
+"token=ee450692cbd24e8bacb27b6f46b4d752&modelId="+modelId
+"&regDate="+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;
}
}