Java tutorial
package xqpark; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.security.MessageDigest; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; import org.apache.http.ParseException; import org.json.JSONException; import org.json.JSONObject; public class ParkApi { static String jhappid = "xqtc"; static String key = "jhkj"; static String URL = "http://dev.xqpark.cn/xqtcApp"; static String time = ""; /** * API? * @param ?url * @return APIJSON? * @throws JSONException * @throws IOException * @throws ParseException */ public static JSONObject loadJSON(String url) throws JSONException { StringBuilder json = new StringBuilder(); try { URLEncoder.encode(url, "UTF-8"); URL urlObject = new URL(url); HttpURLConnection uc = (HttpURLConnection) urlObject.openConnection(); uc.setDoOutput(true);// URL uc.setDoInput(true);// URL uc.setUseCaches(false); uc.setRequestMethod("POST"); BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "utf-8")); String inputLine = null; while ((inputLine = in.readLine()) != null) { json.append(inputLine); } in.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } JSONObject responseJson = new JSONObject(json.toString()); return responseJson; } /** * ?URL * @param urlStr?? * @return get??????keyvalue alue?MD5? * @throws UnsupportedEncodingException */ public static String getHashCode(String urlStr) throws UnsupportedEncodingException { String hashStr = ""; Map<String, String> keyMap = new TreeMap<String, String>(); time = Long.toString(System.currentTimeMillis()); String[] strarray = urlStr.split("&"); for (String temp : strarray) { if (temp.split("=").length == 2) { String value = temp.split("=")[1]; keyMap.put(temp.split("=")[0], value); } keyMap.put("jhappid", jhappid); keyMap.put("time", time); } Set<String> keySet = keyMap.keySet(); Iterator<String> iter = keySet.iterator(); while (iter.hasNext()) { String key = iter.next(); hashStr += keyMap.get(key); } hashStr += key; // System.out.println(hashStr); return hashStr; } /** * MD5? * @param str:?MD5? * @return ?? */ public static String getMD5Code(String str) throws UnsupportedEncodingException { MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(); return ""; } byte[] byteArray = str.getBytes("UTF-8"); byte[] md5Bytes = md5.digest(byteArray); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) { hexValue.append("0"); } hexValue.append(Integer.toHexString(val)); } // System.out.println(hexValue.toString()); return hexValue.toString(); } /** * ??URL * @param params */ public static String getActionUrl(String params) throws Exception { String turl = null, hash = null; // String params = "service=getCarOwnerInfo&username="+user; String action = params.substring(params.indexOf('=') + 1, params.indexOf('&')); // "http://api.map.baidu.com/telematics/v3/weather?location=%E6%88%90%E9%83%BD&output=json&ak=rnm8udmHdWaHFWZTO2tuTiG8"; hash = getMD5Code(getHashCode(params)); turl = params + "&time=" + time + "&jhappid=" + jhappid + "&hash=" + hash; String actionUrl = URL + "/main/" + action + ".action?" + turl; return actionUrl; } /******************************************** *************************************************/ /** * ??? * @param phone * @param type:1 2? 3? * @param usertype * return {"msg":"????","code":1,"vcode":"885316"} */ public static JSONObject getVerificationCode(String phone, String type, String usertype) throws JSONException, Exception { String params = "service=getVerificationCode&phone=" + phone + "&type=" + type + "&usertype=" + usertype; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * * @param username?? * @param password? * @param vcode??n * return {"msg":"?!","code":1,"info":{"blance":0,"pidList":[],"accesstoken":"","ifexamine":0,"xcoState":0,"userid":13131,"phonetype":1,"citycode":"","coupons":"?10","nickname":"159****5104","drivinglicenseurl":"","channelId":0,"email":"","borth":"","sex":0,"headurl":"","identityurl":"","usertype":0,"userId":"","platenumber":"","childaccount":0,"registertime":1444189957449,"duty":0,"parkdistance":"","username":"15988845104"}} */ public static JSONObject register(String username, String password, String vcode) throws JSONException, Exception { String passwordTemp = getMD5Code(password); String params = "service=register&username=" + username + "&password=" + passwordTemp + "&usertype=1&phonetype=3&vcode=" + vcode + "&token=123456&spcode=XMLS"; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * * @param username * @param password * return {"msg":"?","code":1,"info":{"blance":0,"pidList":[],"accesstoken":"","ifexamine":0,"xcoState":1,"userid":13131,"phonetype":1,"citycode":"","coupons":"","nickname":"159****5104","drivinglicenseurl":"","channelId":0,"email":"","borth":"","sex":0,"headurl":"","identityurl":"","usertype":1,"userId":"","platenumber":"","childaccount":0,"registertime":1444189957000,"duty":0,"parkdistance":"","username":"15988845104"}} */ public static JSONObject login(String username, String password) throws JSONException, Exception { String passwordTemp = getMD5Code(password); String params = "service=login&username=" + username + "&password=" + passwordTemp + "&usertype=1&phonetype=1&token=123456"; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ?? * @param user:???? * @return JSON? */ public static JSONObject getCarOwnerInfo(String username) throws JSONException, Exception { String params = "service=getCarOwnerInfo&username=" + username; // String url = // "http://api.map.baidu.com/telematics/v3/weather?location=%E6%88%90%E9%83%BD&output=json&ak=rnm8udmHdWaHFWZTO2tuTiG8"; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ??? * @param userid */ public static JSONObject getConsumeRecordes(String userid) throws JSONException, Exception { String params = "service=getConsumeRecordes&userid=" + userid + "&type=2&page=1"; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ???? * @param userid * @param totalfee * @param type * @param orderid */ public static JSONObject getAlSign(String userid, String totalfee, String type, String orderid) throws JSONException, Exception { String params = "service=getAlSign&userid=" + userid + "&totalfee=" + totalfee + "&subject=&body=&type=" + type + "&orderid=" + orderid; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ???type=2,usertype=1,vcode??? * @param username * @param vcode * @param password ?? * return {"msg":"??","code":1} */ public static JSONObject changePassWord(String username, String vcode, String password) throws JSONException, Exception { String passwordTemp = getMD5Code(password); String params = "service=changePassWord&username=" + username + "&vcode=" + vcode + "&password=" + passwordTemp + "&type=1"; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * * @param userid * return {"msg":"?","code":1} */ public static JSONObject exit(String userid) throws JSONException, Exception { String params = "service=exit&userid=" + userid + "&type=1"; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ? * @param userid * @param nickname * @param sex 10 * @param born * @param email * return {"msg":"??","code":1} */ public static JSONObject editCarOwnerInfo(String userid, String nickname, String sex, String born, String email) throws JSONException, Exception { String params = "service=editCarOwnerInfo&userid=" + userid + "&nickname=" + nickname + "&sex=" + sex + "&headPic=&born=" + born + "&email=" + email + "&platenumber=&drivinglicense="; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ()(??) * @param file:? * @param type:1?2c?4?5?6??7 * @param userid:id?type=7? * @return */ public static JSONObject upload(String file, String type, String userid) throws JSONException, Exception { String params = "service=upload&file=&type=1&userid=7&parkid=&url="; String actionUrl = getActionUrl(params).replace("/main", ""); System.out.println(actionUrl); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ??? * @param parkid * @return {"msg":"??","code":0,"info":null} */ public static JSONObject getParkInfo(String parkid) throws JSONException, Exception { String params = "service=getParkInfo&parkid=" + parkid + "&userid="; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ?? * @param userid:id(type=2,id?id) * @param type:1 2?(type=2,id?id)?? * return {"msg":"","code":1,"count":0,"list":[]} */ public static JSONObject getOrders(String userid, String type) throws JSONException, Exception { String params = "service=getOrders&userid=" + userid + "&type=" + type + "&page=1&state="; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ?? * @param parkid:?id * @param useridid * @param content * @param score * @param orderid?? */ public static JSONObject doParkAppraise(String parkid, String userid, String content, String score, String orderid) throws JSONException, Exception { String nickname = "?"; String parkname = "?"; String params = "service=doParkAppraise&parkid=" + parkid + "&userid=" + userid + "&nickname=" + nickname + "&content=" + content + "&score=" + score + "&orderid=" + orderid + "&parkname=" + parkname; String actionUrl = getActionUrl(params); System.out.println(actionUrl); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ??? * @param parkid * return {"msg":"","code":1,"count":0,"list":[]} */ public static JSONObject getParkAppraise(String parkid) throws JSONException, Exception { String params = "service=getParkAppraise&parkid=" + parkid + "&page=1"; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ??? * @param parkid * @param userid * @param type:1? 2?? * @return {"msg":"??","code":1} */ public static JSONObject doCollect(String parkid, String userid, String type) throws JSONException, Exception { String params = "service=doCollect&parkid=" + parkid + "&parkid=" + parkid + "&userid=" + userid + "&type=" + type; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ??? * @param userid * @param page:??(?10?) * return {"msg":"","code":1,"count":0,"list":[]} */ public static JSONObject getCollects(String userid, String page) throws JSONException, Exception { String params = "service=getCollects&userid=" + userid + "&page=" + page; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * * @param parkid * @param userid * @param content(500) * @return {"msg":"??","code":0} */ public static JSONObject doComplain(String parkid, String userid, String content) throws JSONException, Exception { String params = "service=doComplain&parkid=" + parkid + "&userid=" + userid + "&content=" + content; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ?? * @param orderid * @return {"msg":"","code":1,"info":null} */ public static JSONObject getOrderInfo(String orderid) throws JSONException, Exception { String params = "service=getOrderInfo&orderid=" + orderid; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ?(?) * @param searchkey:? (eg:?) * @param userid * return {"code":1,"count":38,"list":[...],"msg":"???"} */ public static JSONObject searchPark(String searchkey, String userid) throws JSONException, Exception { String params = "service=searchPark&searchkey=" + searchkey + "&userid" + userid; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ? * @param parkid * @param userid * @param starttime:2015-5-21 11:33 * @param endtime:?2015-5-22 11:33 * @param type:1??2? * @param prepay:? * @param parktype:??1 2 * @param platenumber:?? * return */ public static JSONObject doParkReservation(String parkid, String userid, String starttime, String endtime, String type, String prepay, String parktype, String platenumber) throws JSONException, Exception { String params = "service=doParkReservation&chargetype=1&parkid=" + parkid + "&userid=" + userid + "&nickname=xq&starttime=" + starttime + "&endtime=" + endtime + "&type=" + type + "&paystate=2&preferentialid=&parktype=" + parktype + "&prepay=" + prepay + "&platenumber=" + platenumber; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * (?)(?????&parkid=8&userid=9????id?id) * @param orderid * @param parkid * @param type :1? 2(?) */ public static JSONObject doPark(String orderid, String parkid, String userid, String type) throws JSONException, Exception { String params = "service=doPark&orderid=" + orderid + "&parkid=" + parkid + "&userid" + userid + "&type=" + type; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * (??) * @param orderid * @param parkid * @param userid * @param type:1? 2(??) */ public static JSONObject endPark(String orderid, String parkid, String userid, String type) throws JSONException, Exception { String params = "service=endPark&orderid=" + orderid + "&parkid=" + parkid + "&userid" + userid + "&type=" + type; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ? * @param starttime:(2015-04-25 12:33:22) * @param endtime * @param rule:(50,80,100,120,125,20) * @param type:12:24? */ public static JSONObject getTransactionValue(String starttime, String endtime, String rule, String type) throws JSONException, Exception { String params = "service=getTransactionValue&starttime=" + starttime + "&endtime=" + endtime + "&rule=" + rule + "&type=" + type; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ?? * @param userid * @param money:??? * @param zfbaccount:???? * @param zfbname???? */ public static JSONObject withdrawalsMoney(String userid, String money, String zfbaccount, String zfbname) throws JSONException, Exception { String params = "service=withdrawalsMoney&userid=" + userid + "&money=" + money + "&zfbaccount=" + zfbaccount + "&zfbname=" + zfbname; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ? * @param userid * @param platenumber:? */ public static JSONObject addPlatenumber(String userid, String platenumber) throws JSONException, Exception { String params = "service=addPlatenumbery&userid=" + userid + "&platenumber=" + platenumber; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ? * @param orderid * @param userid * @param paytype:1:? 2:(?) */ public static JSONObject doCharged(String orderid, String userid, String paytype) throws JSONException, Exception { String params = "service=doCharged&orderid=" + orderid + "&userid=" + userid + "&paytype=" + paytype; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ?? * @param orderid * @param userid */ public static JSONObject cancelOrder(String orderid, String userid) throws JSONException, Exception { String params = "service=cancelOrder&orderid=" + orderid + "&userid=" + userid; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ?? * @param userid */ public static JSONObject getParkOwnerInfo(String userid) throws JSONException, Exception { String params = "service=cancelOrder&userid=" + userid; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ?? * @param parkid * @param type:1?,2??,3?,4????(?) */ public static JSONObject getParkPictures(String parkid, String type) throws JSONException, Exception { String params = "service=getParkPictures&parkid=" + parkid + "&type=" + type; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ? * @param username * @param vcode:??? * @param type12 */ public static JSONObject doEasiestLogin(String username, String vcode, String type) throws JSONException, Exception { String params = "service=doEasiestLogin&username=" + username + "&vcode=" + vcode + "&type=" + type + "&phonetype=3"; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * * @param userid * @param usertype:1,2 * @param content:???500 */ public static JSONObject doFeedBack(String userid, String usertype, String content) throws JSONException, Exception { String params = "service=doFeedBack&userid=" + userid + "&usertype=" + usertype + "&content=" + content; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ????(1) * @param type:1???,2???(???) */ public static JSONObject getSearchKeyNew(String type) throws JSONException, Exception { String params = "service=getSearchKeyNew&type=" + type; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ? * @param userid */ public static JSONObject getCoupons(String userid) throws JSONException, Exception { String params = "service=getCoupons&userid=" + userid; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ????? * @param city:?? * @param scope:?(??) */ public static JSONObject getParkByCoordinate(String city, String scope) throws JSONException, Exception { String params = "service=getParkByCoordinate&city=" + city + "&scope=" + scope; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ?? * @param out_trade_no:?? * @param trade_no:? * @param total_fee:? */ public static JSONObject wxPaySuccess(String out_trade_no, String trade_no, String total_fee) throws JSONException, Exception { String params = "service=wxPaySuccess&out_trade_no=" + out_trade_no + "&trade_no=" + trade_no + "total_fee" + total_fee; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ?? * @param parkid * @param type :1,2 * @param page :??(?10?) */ public static JSONObject getToDoList(String parkid, String type, String page) throws JSONException, Exception { String params = "service=getToDoList&parkid=" + parkid + "&type=" + type + "page" + page; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ?? * @param parkid */ public static JSONObject getToDoListCount(String parkid) throws JSONException, Exception { String params = "service=getToDoListCount&parkid=" + parkid; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ??(????http://dev.xqpark.cn/xqtcApp/alipayapi.jsp) * @param out_trade_no :??? * @param total_fee :? public static JSONObject getToDoListCount(String out_trade_no,String total_fee) throws JSONException, Exception { String params = "service=getToDoListCount&out_trade_no="+out_trade_no+"total_fee"+total_fee; String actionUrl = URL+"/alipayapi.jsp?"+params; JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } */ /** * ??() * @return */ public static JSONObject getAdvertPictures() throws JSONException, Exception { String params = "service=getAdvertPictures&type="; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ??? * @param cityid :? */ public static JSONObject getAirportIntroduction(String cityid) throws JSONException, Exception { String params = "service=getAirportIntroduction&cityid=" + cityid; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ???? * @param phone :? * @param userid :id * return :{"msg":"","code":1,"peccancyurl":"http://wap.cx580.com/illegal?user_id=13131..."} */ public static JSONObject getPeccancy(String phone, String userid) throws JSONException, Exception { String params = "service=getPeccancy&phone=" + phone + "&userid=" + userid; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } /** * ?(?) * @param mobile :??? */ public static JSONObject getUserSex(String mobile) throws JSONException, Exception { String params = "service=getUserSex&mobile=" + mobile; String actionUrl = getActionUrl(params); JSONObject jsonObject = loadJSON(actionUrl); return jsonObject; } }