您的位置:首页 > 其它

免费的车辆违章车首页接口封装

2016-03-08 22:57 211 查看
车首页提供的违章查询接口类:

package carwz;

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.math.BigInteger;

import java.net.HttpURLConnection;

import java.net.URL;

import java.net.URLEncoder;

import java.security.MessageDigest;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

* title:车首页违章查询接口

*

*

*/

public class CheShouYeApiTest {

public static String URL =”http://www.cheshouye.com”;

/**
* title:测试方法
*
* @param args
*/


// public static void main(String[] args) {

// // TODO Auto-generated method stub

//

// try {

// String carInfo = “{hphm=粤B12345&classno=123456&engineno=1234&city_id=152&car_type=02}”;

// String appId=”100”; //联系车首页获取

// String appKey=”123456”;//联系车首页获取

//

// System.out.println(“请稍后…”);

// String sb = getWeizhangInfoPost(carInfo, appId, appKey);

// System.out.println(“返回违章结果:” + sb);

//

//

// //获取省份城市字典配置

// System.out.println(“附录一 获取省份城市参数接口参数=”+getConfig());

//

// } catch (Exception e) {

// e.printStackTrace();

// }

// }

/**
* title:获取违章信息
*
* @param carInfo
* @return
*/
public static String getWeizhangInfoPost(String carInfo, String appId, String appKey) {
long timestamp = System.currentTimeMillis();

String line = null;
String signStr = appId + carInfo + timestamp + appKey;
String sign = md5(signStr);
try {
URL postUrl = new URL(URL + "/api/weizhang/query_task?");
String content = "car_info=" + URLEncoder.encode(carInfo, "utf-8") + "&sign=" + sign + "×tamp=" + timestamp + "&app_id=" + appId;

System.out.println("请求URL="+postUrl+content);

line = post(postUrl, content);
} catch (Exception e) {
e.printStackTrace();
}
return line;
}

/**
* title:获取省份城市对应ID配置
*
* @return
* @throws IOException
*/
public static String getConfig() throws IOException {
String line = null;
try {
URL postUrl = new URL(URL + "/api/weizhang/get_all_config?");
String content = "";
line = post(postUrl, content);
} catch (Exception e) {
e.printStackTrace();
}
return line;

}

/**
* title:post请求
*
* @param postUrl
* @param content
* @return
*/
private static String post(URL postUrl, String content) {
String line = null;
try {
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(content);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));

while ((line = reader.readLine()) != null) {
return line;
}
reader.close();
connection.disconnect();

} catch (Exception e) {
e.printStackTrace();
}
return line;

}

/**
* title:md5加密,应与 (http://tool.chinaz.com/Tools/MD5.aspx) 上32加密结果一致
*
* @param password
* @return
*/
private static String md5(String msg) {
try {
MessageDigest instance = MessageDigest.getInstance("MD5");
instance.update(msg.getBytes("UTF-8"));
byte[] md = instance.digest();
return byteArrayToHex(md);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

private static String byteArrayToHex(byte[] a) {
StringBuilder sb = new StringBuilder();
for (byte b : a) {
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}


}

//车辆信息类

package carwz;

/*

* hphm 车牌号码

classno 车架号

engineno 发动机号

registno 证书编号

city_id 违章查询地城市ID

car_type 车辆车类型(”02”:小型汽车)

appId

appKey需要到车首页申请

* */

public class CarData {

private String mHphm;

private String mClassno;

private String mEngineno;

private String registno;

private String mCity_id;

private String mType;

private String appId=”*“;

private String appKey=”**“;

private StringBuilder carInfo = new StringBuilder();

public CarData(String hphm,String classno,String engineno,String city_id,String car_type){

carInfo.append(“{“);

carInfo.append(“hphm=”+hphm+”&”+”classno=”+classno+

“&engineno=”+engineno+”&”+”city_id=”+city_id+”&car_type=”+car_type);

carInfo.append(“}”);

System.out.println(carInfo.toString());

}

public String getWzInfo(){
return CheShouYeApiTest.getWeizhangInfoPost(carInfo.toString(), appId, appKey).toString();
}


}

//违章信息类

package carwz;

import java.util.ArrayList;

//违章信息

public class WeiZhangInfo {

private int status;//处理状态

private int total_score;//扣分总计

private int total_money;//罚款总计

private int count;//违章记录条数

private ArrayList historys;//位置记录数组

public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public int getTotal_score() {
return total_score;
}
public void setTotal_score(int total_score) {
this.total_score = total_score;
}
public int getTotal_money() {
return total_money;
}
public void setTotal_money(int total_money) {
this.total_money = total_money;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public ArrayList<History> getHistorys() {
return historys;
}
public void setHistorys(ArrayList<History> historys) {
this.historys = historys;
}


}

//单条违章记录类

package carwz;

//单条违章记录信息

public class History {

private int id;//位置记录编号

private int car_id;//汽车编号

private String status;//违章记录是否处理(”Y”:已处理,”N”:未处理)

private int fen;//违章扣分

private String officer;//执法机关

private String occur_date;//违章时间

private String occur_area;//违章所在地

private int city_id; //违章所在的城市(与省市配置对应)

private int province_id; //违章所在的省份(与省市配置对应)

private String code; //违章代码

private String info; //违章内容

private int money; //罚款金额

private String archive; //证书编号

private String city_name;//违章所在的城市名称(与省市配置对应)

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCar_id() {
return car_id;
}
public void setCar_id(int car_id) {
this.car_id = car_id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getFen() {
return fen;
}
public void setFen(int fen) {
this.fen = fen;
}
public String getOfficer() {
return officer;
}
public void setOfficer(String officer) {
this.officer = officer;
}
public String getOccur_date() {
return occur_date;
}
public void setOccur_date(String occur_date) {
this.occur_date = occur_date;
}
public String getOccur_area() {
return occur_area;
}
public void setOccur_area(String occur_area) {
this.occur_area = occur_area;
}
public int getCity_id() {
return city_id;
}
public void setCity_id(int city_id) {
this.city_id = city_id;
}
public int getProvince_id() {
return province_id;
}
public void setProvince_id(int province_id) {
this.province_id = province_id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public String getArchive() {
return archive;
}
public void setArchive(String archive) {
this.archive = archive;
}
public String getCity_name() {
return city_name;
}
public void setCity_name(String city_name) {
this.city_name = city_name;
}

public String toString(){
return info;
}


}

//违章信息查询工具类

package carwz;

import java.util.ArrayList;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

public class WeiZhangUtils {

private static String wzinfo;

//解析出违章记录类
public static WeiZhangInfo getWeiZhangInfo(String hphm,String classno,String engineno,String city_id,String car_type){
String wzjsonStr = new CarData(hphm,classno,engineno,city_id,car_type)
.getWzInfo().toString();

if(wzjsonStr == null || wzjsonStr.equals("")){
return null;
}
JSONObject jo = JSONObject.fromObject(wzjsonStr);
WeiZhangInfo wzInfo = new WeiZhangInfo();

wzInfo.setStatus(jo.getInt("status"));
wzInfo.setCount(jo.getInt("count"));
wzInfo.setTotal_money(jo.getInt("total_money"));
wzInfo.setTotal_score(jo.getInt("total_score"));
wzInfo.setHistorys(getHistorys(jo.getString("historys")));
return wzInfo;
}

//解析出违章记录数组
private static ArrayList<History> getHistorys(String historysJsonStr){
if(historysJsonStr == null || historysJsonStr.equals("")){
return null;
}
JSONArray ja = JSONArray.fromObject(historysJsonStr);
ArrayList<History> historys = new ArrayList<History>();
for(int i = 0; i < ja.size(); i++){
JSONObject object = (JSONObject)ja.get(i);
History history = (History)JSONObject.toBean(object, History.class);
if(history != null){
historys.add(history);
}
}
return historys;
}


}

//测试类

package carwz;

import java.io.IOException;

import java.util.ArrayList;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

public class Application {

public static void main(String[] args){

WeiZhangInfo wzInfo = WeiZhangUtils.getWeiZhangInfo("车牌号","车架号","发动机引擎号","城市ID号","02 车辆类型:大型车01 小型车02");
System.out.print("wzInfo history length:"+ wzInfo.getHistorys().size());
for(int i = 0; i < wzInfo.getHistorys().size(); i++){
System.out.println("违章信息:"+wzInfo.getHistorys().get(i).toString());
}
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: