测试接口

This commit is contained in:
zhouyahui 2018-12-04 18:11:59 +08:00
parent 4794dd0e20
commit 00f0e50a12

View File

@ -5,54 +5,73 @@ 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 String getVehicleAppraisementResponse(String modelId,String zone,String regDate,String mile){
String urlStr = InitSecondHandCarConfig.PREFIX+"/service/getUsedCarPrice?"
+"token="+InitSecondHandCarConfig.TOKEN+"&modelId="+modelId
+"&regDate="+regDate+"&mile="+mile+"&zone="+zone+"";
String result = VehicleAppraisementUtil.getResponseResult(urlStr);
return result;
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&regDate=" + 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 + "&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();
}
//通过请求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;
}
} 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;
}
}