由于公司最近要求和口袋通对接微商城,所以用到了口袋通的API,其实和微信开放平台的API很像,那么在上代码之前先说说口袋通吧,口袋通也是随着微信的浪潮诞生自白鸦之手,都是基于微信开放平台,对微信公众账号做二次开发,不过目前口袋通完全免费,和他们对接的过程中,有过一些沟通,反馈挺快的。
- /**
- *
- * @Description: 口袋通对接工具类
- * @author zhangJun
- * @date 2014-1-23 下午1:52:29
- */
- public class KouDaiTongUtil {
- public static final String KDT_URL="http://open.koudaitong.com/api/entry";
- public static final String APP_ID="";
- public static final String SECRET="";
- public static final String FORMAT="json"; //响应格式
- public static final String SIGN_METHOD="md5"; //参数的加密方法
- public static final String V="1.0";//API协议版本
- /**
- * 通用构造API请求参数
- * @param appId 在口袋通后台获取
- * @param format 可选,指定响应格式。默认json,目前支持格式为json
- * @param method 调用API的接口名称,参照口袋通
- * @param signMethod 可选,参数的加密方法选择。默认为md5
- * @param timestamp 时间戳 方法内部会处理成格式为yyyy-mm-dd HH:mm:ss
- * @param v API协议版本,可选值:1.0
- * @param paramMap(调用api的参数,key为参数名称,需要和调用口袋通方法参数名称相同,否则调用不成功)
- * @return
- * @throws UnsupportedEncodingException
- */
- public static String constructParam(String appId,String secret,String format,String method,String signMethod,Date timestamp,String v,Map<String,Object> paramMap) throws UnsupportedEncodingException {
- String timestampStr=DateUtils.DateToString(timestamp, DateUtils.YYYY_MM_DD_HH_MM_SS);
- //遵循API对参数进行升序排列
- Map<String,Object> map=new TreeMap<String,Object>();
- //通讯系统参数
- map.put("app_id", appId);
- map.put("format", format);
- map.put("method", method);
- map.put("sign_method", signMethod);
- map.put("timestamp", timestampStr);
- map.put("v", v);
- //应用参数
- map.putAll(paramMap);
- StringBuffer systemStr=new StringBuffer();
- //头加secret
- systemStr.append(SECRET);
- Set<Map.Entry<String, Object>> set=map.entrySet();
- for(Iterator<Map.Entry<String, Object>> it= set.iterator();it.hasNext();){
- Map.Entry<String, Object> entry=it.next();
- systemStr.append(entry.getKey()+entry.getValue());
- }
- //尾加secret
- systemStr.append(SECRET);
- //拼接应用参数
- Set<Map.Entry<String, Object>> paramSet=paramMap.entrySet();
- StringBuffer appStr=new StringBuffer();
- for(Iterator<Map.Entry<String, Object>> it=paramSet.iterator();it.hasNext();){
- appStr.append("&");
- Map.Entry<String, Object> entry=it.next();
- if(containWhiteSpace((String)entry.getValue().toString())){
- appStr.append(entry.getKey()+"="+URLEncoder.encode((String)entry.getValue(),"utf-8"));
- }else{
- appStr.append(entry.getKey()+"="+entry.getValue());
- }
- }
- //32位md5加密
- String sign=Md5CyptoUtil.md5(systemStr.toString());
- String param="sign="+sign+"×tamp="+URLEncoder.encode(timestampStr,"utf-8")+"&v="+v+"&app_id="+APP_ID+"&method="+method+"&sign_method="+signMethod+"&format="+format+appStr;
- return param;
- }
-
-
- /**
- *
- * @param 判断是否包含空字符串
- * @return
- */
- public static boolean containWhiteSpace(String input){
- Pattern pattern = Pattern.compile("\\s");
- Matcher matcher = pattern.matcher(input);
- boolean flag = matcher.find();
- return flag;
- }
- /**
- * 发送get请求
- * @param url
- * @param param
- * @return
- */
- public static String sendGet(String url,String param) {
- String result = "";
- BufferedReader in = null;
- try {
- String urlNameString = url + "?" + param;
- URL realUrl = new URL(urlNameString);
- URLConnection connection = realUrl.openConnection();
- connection.connect();
- in = new BufferedReader(new InputStreamReader(
- connection.getInputStream()));
- String line;
- while ((line = in.readLine()) != null) {
- result += line;
- }
- } catch (Exception e) {
- System.out.println("发送GET请求出现异常!" + e);
- e.printStackTrace();
- }
- finally {
- try {
- if (in != null) {
- in.close();
- }
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return result;
- }
-
- /**
- * 口袋同通用请求方法返回json字符串
- * @param method 调用API的接口名称来源KdtMethodListEnum
- * @param paramMap(调用api的参数,key为参数名称,需要和调用口袋通方法参数名称相同,否则调用不成功)
- * @return
- * @throws UnsupportedEncodingException
- */
- public static Map<String,Object> sendRequest(String method,Map<String,Object> paramMap) throws UnsupportedEncodingException{
- Map<String,Object> resultMap=new HashMap<String,Object>();
- String param = constructParam(APP_ID,SECRET, FORMAT, method, SIGN_METHOD, new Date(), V, paramMap);
- String reponse= sendGet(KDT_URL,param);
- JSONObject jsonObject=JSONObject.fromObject(reponse);
- //请求发生错误
- if(jsonObject.containsKey("error_response")){
- int errorCode=Integer.parseInt(JSONObject.fromObject(jsonObject.get("error_response")).get("code").toString());
- String msg=JSONObject.fromObject(jsonObject.get("error_response")).get("msg").toString();
- KdtErrorCodeEnum codeEnum=KdtErrorCodeEnum.getKdtErrorCodeEnumByCode(errorCode);
- resultMap.put("status", "FAILED");
- resultMap.put("message","请求失败,"+msg+",错误码:"+errorCode+"错误名称:"+codeEnum.name+"解决方案:"+codeEnum.scheme);
- }else{
- resultMap.put("status", "SUCCESS");
- resultMap.put("result", reponse);
- }
- return resultMap;
- }
测试方法
- /**
- * 获取交易订单(如果参数都不传就会下拉所有订单)
- * @param status 订单状态
- * @param startCreated 订单创建起始时间
- * @param endCreated 订单创建结束时间
- * @param startPayDate 支付起始时间
- * @param endPayDate 支付结束时间
- * @param buyerNick 昵称
- * @param pageNo 页数
- * @param pageSize 每页条数
- * @return
- * @throws UnsupportedEncodingException
- */
- public Map<String,Object> getOrderListByCondition(KdtOrderStatusEnum status,Date startCreated,Date endCreated,Date startPayDate,Date endPayDate,String buyerNick,Integer pageNo,Integer pageSize) throws UnsupportedEncodingException{
- String method=KdtMethodListEnum.KDT_TRADES_SOLD_GET.vlaue; //查询卖家已卖出的交易列表
- Map<String,Object> paramMap=new HashMap<String,Object>();
- if(status!=null){
- paramMap.put("status", status.type);
- }
- if(startCreated!=null){
- paramMap.put("start_created", DateUtils.DateToString(startCreated,DateUtils.YYYY_MM_DD_HH_MM_SS));
- }
- if(endCreated!=null){
- paramMap.put("end_created", DateUtils.DateToString(endCreated,DateUtils.YYYY_MM_DD_HH_MM_SS));
- }
- if(startPayDate!=null){
- paramMap.put("start_pay", DateUtils.DateToString(startPayDate,DateUtils.YYYY_MM_DD_HH_MM_SS));
- }
- if(endPayDate!=null){
- paramMap.put("end_pay", DateUtils.DateToString(endPayDate,DateUtils.YYYY_MM_DD_HH_MM_SS));
- }
- if(!StringUtils.isBlank(buyerNick)){
- paramMap.put("buyer_nick", buyerNick);
- }
- if(pageNo!=null){
- paramMap.put("page_no", pageNo);
- }
- if(pageSize!=null){
- paramMap.put("page_size", pageSize);
- }
- Map<String,Object> map=KouDaiTongUtil.sendRequest(method,paramMap);
- return map;
- }