public Reader getReader(Charset charset) { if (charset == null) { throw new IllegalArgumentException("charset can not be null"); } return getReader(charset.name()); } public Cookie getCookie() {
public void print(Writer writer, String charsetName) { Reader reader = null; try { if (null == charsetName) reader = getReader(); else reader = this.getReader(charsetName); int c; char[] buf = new char[8192]; while (-1 != (c = reader.read(buf))) { writer.write(buf, 0, c); } writer.flush(); } catch (IOException e) { throw Lang.wrapThrow(e); } }
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { NutMap jreq = new NutMap(); jreq.setv("jsonrpc", "2.0").setv("id", R.UU32()).setv("method", method.getName()); if (!Strings.isBlank(namespace)) { jreq.put("namespace", namespace); } jreq.setv("params", args); Request req = Request.create(endpoint, METHOD.POST); req.setData(Json.toJson(jreq)); Response resp = Sender.create(req).setTimeout(timeout).send(); if (resp.isOK()) { if (method.getReturnType() == Void.class) return null; return Json.fromJson(method.getGenericReturnType(), resp.getReader()); } throw new RuntimeException("resp code="+resp.getStatus()); } });
public Reader getReader(Charset charset) { if (charset == null) { throw new IllegalArgumentException("charset can not be null"); } return getReader(charset.name()); } public Cookie getCookie() {
public void print(Writer writer, String charsetName) { Reader reader = null; try { if (null == charsetName) reader = getReader(); else reader = this.getReader(charsetName); int c; char[] buf = new char[8192]; while (-1 != (c = reader.read(buf))) { writer.write(buf, 0, c); } writer.flush(); } catch (IOException e) { throw Lang.wrapThrow(e); } }
@Override public String mediaUpload(String type, File f) { if (type == null) throw new NullPointerException("media type is NULL"); if (f == null) throw new NullPointerException("meida file is NULL"); String url = String.format("http://file.api.weixin.qq.com/cgi-bin/media/upload?token=%s&type=%s", getAccessToken(), type); Request req = Request.create(url, METHOD.POST); req.getParams().put("media", f); Response resp = new FilePostSender(req).send(); if (!resp.isOK()) throw new IllegalStateException("media upload file, resp code=" + resp.getStatus()); Map<String, Object> map = (Map<String, Object>) Json.fromJson(resp.getReader()); if (map != null && map.containsKey("errcode") && ((Number) map.get("errcode")).intValue() != 0) { throw new IllegalArgumentException(map.toString()); } return map.get("media_id").toString(); }
@Override public WxResp media_upload(String type, File f) { if (type == null) throw new NullPointerException("media type is NULL"); if (f == null) throw new NullPointerException("meida file is NULL"); String url = String.format("%s/cgi-bin/media/upload?access_token=%s&type=%s", wxBase, getAccessToken(), type); Request req = Request.create(url, METHOD.POST); req.getParams().put("media", f); Response resp = new FilePostSender(req).send(); if (!resp.isOK()) throw new IllegalStateException("media upload file, resp code=" + resp.getStatus()); return Json.fromJson(WxResp.class, resp.getReader("UTF-8")); }
@Override public WxResp uploadimg(File f) { if (f == null) throw new NullPointerException("meida file is NULL"); String url = String.format("%s/cgi-bin/media/uploadimg?access_token=%s", wxBase, getAccessToken()); Request req = Request.create(url, METHOD.POST); req.getParams().put("media", f); Response resp = new FilePostSender(req).send(); if (!resp.isOK()) throw new IllegalStateException("uploadimg, resp code=" + resp.getStatus()); return Json.fromJson(WxResp.class, resp.getReader("UTF-8")); }
protected Map<String, Object> call(String URL, METHOD method, String body) { String token = getAccessToken(); if (URL.contains("?")) { URL = base + URL + "&access_token=" + token; } else { URL = base + URL + "?access_token=" + token; } Request req = Request.create(URL, method); if (body != null) req.setData(body); Response resp = Sender.create(req).send(); if (!resp.isOK()) throw new IllegalArgumentException("resp code=" + resp.getStatus()); Map<String, Object> map = (Map<String, Object>) Json.fromJson(resp.getReader()); if (map != null && map.containsKey("errcode") && ((Number) map.get("errcode")).intValue() != 0) { throw new IllegalArgumentException(map.toString()); } return map; }
@Override public WxResp kfaccount_uploadheadimg(String kf_account, File f) { if (f == null) throw new NullPointerException("meida file is NULL"); String url = String.format("%s/customservice/kfaccount/uploadheadimg?access_token=%s", wxBase, getAccessToken()); Request req = Request.create(url, METHOD.POST); req.getParams().put("media", f); Response resp = new FilePostSender(req).send(); if (!resp.isOK()) throw new IllegalStateException("uploadimg, resp code=" + resp.getStatus()); return Json.fromJson(WxResp.class, resp.getReader("UTF-8")); }
@Override public WxResp add_material(String type, File f) { if (f == null) throw new NullPointerException("meida file is NULL"); String url = String.format("%s/cgi-bin/material/add_material?access_token=%s&type=%s", wxBase, getAccessToken(), type); Request req = Request.create(url, METHOD.POST); req.getParams().put("media", f); Response resp = new FilePostSender(req).send(); if (!resp.isOK()) throw new IllegalStateException("add_material, resp code=" + resp.getStatus()); return Json.fromJson(WxResp.class, resp.getReader("UTF-8")); }
@Override public WxResp add_video(File f, String title, String introduction) { if (f == null) throw new NullPointerException("meida file is NULL"); String url = String.format("%s/cgi-bin/material/add_material?type=video&access_token=%s", wxBase, getAccessToken()); Request req = Request.create(url, METHOD.POST); req.getParams().put("media", f); req.getParams() .put("description", Json.toJson(new NutMap().setv("title", title).setv("introduction", introduction), JsonFormat.compact().setQuoteName(true))); Response resp = new FilePostSender(req).send(); if (!resp.isOK()) throw new IllegalStateException("add_material, resp code=" + resp.getStatus()); return Json.fromJson(WxResp.class, resp.getReader("UTF-8")); }
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { NutMap jreq = new NutMap(); jreq.setv("jsonrpc", "2.0").setv("id", R.UU32()).setv("method", method.getName()); if (!Strings.isBlank(namespace)) { jreq.put("namespace", namespace); } jreq.setv("params", args); Request req = Request.create(endpoint, METHOD.POST); req.setData(Json.toJson(jreq)); Response resp = Sender.create(req).setTimeout(timeout).send(); if (resp.isOK()) { if (method.getReturnType() == Void.class) return null; return Json.fromJson(method.getGenericReturnType(), resp.getReader()); } throw new RuntimeException("resp code="+resp.getStatus()); } });
@Override public WxResp userinfo(String openid, String access_token) { // https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID Request req = Request.create("https://api.weixin.qq.com/sns/userinfo", METHOD.GET); NutMap params = new NutMap(); params.put("access_token", access_token); params.put("openid", openid); req.setParams(params); Response resp = Sender.create(req).send(); if (!resp.isOK()) { return null; } return Json.fromJson(WxResp.class, resp.getReader("UTF-8")); }
@Override public WxResp access_token(String code) { Request req = Request.create("https://api.weixin.qq.com/sns/oauth2/access_token", METHOD.GET); NutMap params = new NutMap(); params.put("appid", appid); params.put("secret", appsecret); params.put("code", code); params.put("grant_type", "authorization_code"); req.setParams(params); Response resp = Sender.create(req).send(); if (!resp.isOK()) { return null; } return Json.fromJson(WxResp.class, resp.getReader("UTF-8")); }
@Override public WxResp refresh_token(String refresh_token) { Request req = Request.create("https://api.weixin.qq.com/sns/oauth2/refresh_token", METHOD.GET); NutMap params = new NutMap(); params.put("appid", appid); params.put("secret", appsecret); params.put("refresh_token", refresh_token); params.put("grant_type", "refresh_token"); req.setParams(params); Response resp = Sender.create(req).send(); if (!resp.isOK()) { return null; } return Json.fromJson(WxResp.class, resp.getReader("UTF-8")); }