예제 #1
0
  /** 实时获取token,并更新到application中 */
  public String getTokenReal() {
    String requestUrl =
        tokenUrl + "?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret;
    try {
      // 发送请求,返回json
      TenpayHttpClient httpClient = new TenpayHttpClient();
      httpClient.setReqContent(requestUrl);
      String resContent = "";
      if (httpClient.callHttpPost(requestUrl, "")) {
        resContent = httpClient.getResContent();
        Gson gson = new Gson();
        Map<String, String> map =
            gson.fromJson(resContent, new TypeToken<Map<String, String>>() {}.getType());
        // 判断返回是否含有access_token
        if (map.containsKey("access_token")) {
          // 更新application值
          Token = map.get("access_token");
        } else {
          System.out.println("get token err ,info =" + map.get("errmsg"));
        }
        System.out.println("res json=" + resContent);
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
    return Token;
  }
예제 #2
0
 /** 获取TOKEN,一天最多获取200次,需要所有用户共享值 */
 public String GetToken() {
   String requestUrl =
       tokenUrl + "?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret;
   TenpayHttpClient httpClient = new TenpayHttpClient();
   httpClient.setReqContent(requestUrl);
   if (httpClient.call()) {
     String res = httpClient.getResContent();
     Gson gson = new Gson();
     TreeMap map = gson.fromJson(res, TreeMap.class);
     // 在有效期内直接返回access_token
     if (map.containsKey("access_token")) {
       String s = map.get("access_token").toString();
     }
     // 如果请求成功
     if (null != map) {
       try {
         if (map.containsKey("access_token")) {
           Token = map.get("access_token").toString();
           return this.Token;
         }
       } catch (Exception e) {
         // 获取token失败
         System.out.println("失败:" + map.get("errmsg"));
       }
     }
   }
   return "";
 }
예제 #3
0
 // 提交预支付
 public String sendPrepay(SortedMap packageParams) {
   String prepayid = "";
   // 转换成json
   Gson gson = new Gson();
   /* String postData =gson.toJson(packageParams); */
   String postData = "{";
   Set es = packageParams.entrySet();
   Iterator it = es.iterator();
   while (it.hasNext()) {
     Map.Entry entry = (Map.Entry) it.next();
     String k = (String) entry.getKey();
     String v = (String) entry.getValue();
     if (k != "appkey") {
       if (postData.length() > 1) postData += ",";
       postData += "\"" + k + "\":\"" + v + "\"";
     }
   }
   postData += "}";
   // 设置链接参数
   String requestUrl = this.gateUrl + "?access_token=" + this.Token;
   System.out.println("post url=" + requestUrl);
   System.out.println("post data=" + postData);
   TenpayHttpClient httpClient = new TenpayHttpClient();
   httpClient.setReqContent(requestUrl);
   String resContent = "";
   if (httpClient.callHttpPost(requestUrl, postData)) {
     resContent = httpClient.getResContent();
     Map<String, String> map =
         gson.fromJson(resContent, new TypeToken<Map<String, String>>() {}.getType());
     if ("0".equals(map.get("errcode"))) {
       prepayid = map.get("prepayid");
     } else {
       System.out.println("get token err ,info =" + map.get("errmsg"));
     }
     // 设置debug info
     System.out.println("res json=" + resContent);
   }
   return prepayid;
 }