List of usage examples for com.google.gson JsonObject addProperty
public void addProperty(String property, Character value)
From source file:com.baidu.cc.common.JsonUtils.java
License:Apache License
/** * convert map to json object.// w w w.jav a 2 s . c om * * @param map * key is string value will convert to string * @return {@link JsonObject} */ public static JsonObject fromSimpleMap(Map<String, Object> map) { JsonObject jo = new JsonObject(); if (MapUtils.isEmpty(map)) { return jo; } Iterator<Entry<String, Object>> iter = map.entrySet().iterator(); while (iter.hasNext()) { Entry<String, Object> entry = iter.next(); jo.addProperty(entry.getKey(), String.valueOf(entry.getValue())); } return jo; }
From source file:com.baidu.mywork.util.RemoteHttpUtil.java
License:Apache License
public static void main(String[] args) { try {// w w w . j a v a 2 s .c om // json { JsonObject bodyJson = new JsonObject(); bodyJson.addProperty("uid", "testusername"); bodyJson.addProperty("pic_file", "rcGysFG2xqLaO8tlvW4rFVmVqlnx+4qGewYs8a+enmoZ"); String getResponse = RemoteHttpUtil .fetchJsonHttpResponse("http://10.48.26.196:8080/face/json/postjson", null, bodyJson); System.out.print(getResponse); } // // get // { // String getResponse = // RemoteHttpUtil.fetchSimpleHttpResponse("get", "http://10.48.26.196:8080/face/json/getData", // null, null); // System.out.print(getResponse); // } // // // post // { // Map<String, String> bodyMap = new HashMap<String, String>(); // bodyMap.put("aaa", "goodgod"); // String postResponse = // RemoteHttpUtil.fetchSimpleHttpResponse("post", "http://10.48.26.196:8080/face/json/postData", // null, bodyMap); // System.out.print(postResponse); // } // // // form // { // Map<String, ContentBody> bodyMap = new HashMap<String, ContentBody>(); // bodyMap.put("name", new StringBody("heiheiheihei", ContentType.TEXT_PLAIN)); // bodyMap.put("file2", new FileBody(new File("D:\\temp\\test.txt"))); // bodyMap.put("file3", new FileBody(new File("D:\\temp\\test2.txt"))); // String multiHttpResponse = // RemoteHttpUtil.fetchMultipartHttpResponse("http://10.48.26.196:8080/face/file/uploadMultifile", // null, bodyMap); // System.out.print(multiHttpResponse); // } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.balajeetm.mystique.samples.controller.SampleController.java
License:Open Source License
/** * Serialise./*from w ww. j ava 2s. co m*/ * * @param msg the msg * @return the json object */ @GetMapping(value = { "/gson/serialise" }) public JsonObject serialise(@RequestParam(value = "msg") String msg) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("report", "Gson Serialisation working good!"); jsonObject.addProperty("status", Boolean.TRUE); jsonObject.addProperty("msg", msg); return jsonObject; }
From source file:com.balajeetm.mystique.samples.controller.SampleController.java
License:Open Source License
/** * Deserialise.//from w w w . j a v a2 s. c o m * * @param payload the payload * @return the json element */ @PostMapping(value = { "/gson/deserialise" }) public JsonElement deserialise(@RequestBody JsonElement payload) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("report", "Gson Deserialisation working good!"); jsonObject.addProperty("status", Boolean.TRUE); jsonObject.add("payload", payload); return jsonObject; }
From source file:com.balajeetm.mystique.samples.controller.SampleController.java
License:Open Source License
/** * Error.//w ww . j ava 2 s .c om * * @param e the e * @return the json object */ @ExceptionHandler(value = { Exception.class }) public JsonObject error(Exception e) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("report", "Oops! Something went wrong!"); jsonObject.addProperty("msg", e.getLocalizedMessage()); return jsonObject; }
From source file:com.baomidou.springwind.util.yuntongxin.CCPRestSDK.java
License:Open Source License
/** * ??//from ww w.ja va 2s . c om * * @param date * ? day ??00:00 23:59 * @param keywords * ?? ?????? * @return */ public HashMap<String, Object> billRecords(String date, String keywords) { HashMap<String, Object> validate = accountValidate(); if (validate != null) return validate; if ((isEmpty(date))) { logger.debug("?: "); throw new IllegalArgumentException("?: "); } CcopHttpClient chc = new CcopHttpClient(); DefaultHttpClient httpclient = null; try { httpclient = chc.registerSSL(SERVER_IP, "TLS", Integer.parseInt(SERVER_PORT), "https"); } catch (Exception e1) { //e1.printStackTrace(); logger.error(e1.getMessage()); throw new RuntimeException("?httpclient" + e1.getMessage()); } String result = ""; try { HttpPost httppost = (HttpPost) getHttpRequestBase(1, BillRecords); String requsetbody = ""; if (BODY_TYPE == BodyType.Type_JSON) { JsonObject json = new JsonObject(); json.addProperty("appId", App_ID); json.addProperty("date", date); if (!(isEmpty(keywords))) json.addProperty("keywords", keywords); requsetbody = json.toString(); } else { StringBuilder sb = new StringBuilder("<?xml version='1.0' encoding='utf-8'?><BillRecords>"); sb.append("<appId>").append(App_ID).append("</appId>").append("<date>").append(date) .append("</date>"); if (!(isEmpty(keywords))) sb.append("<keywords>").append(keywords).append("</keywords>"); sb.append("</BillRecords>").toString(); requsetbody = sb.toString(); } logger.info("billRecords Request body = : " + requsetbody); //? System.out.println("" + requsetbody); BasicHttpEntity requestBody = new BasicHttpEntity(); requestBody.setContent(new ByteArrayInputStream(requsetbody.getBytes("UTF-8"))); requestBody.setContentLength(requsetbody.getBytes("UTF-8").length); httppost.setEntity(requestBody); HttpResponse response = httpclient.execute(httppost); status = response.getStatusLine().getStatusCode(); System.out.println("Https??" + status); HttpEntity entity = response.getEntity(); if (entity != null) result = EntityUtils.toString(entity, "UTF-8"); EntityUtils.consume(entity); } catch (IOException e) { e.printStackTrace(); logger.error(e.getMessage()); return getMyError("172001", ""); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); return getMyError("172002", ""); } finally { if (httpclient != null) httpclient.getConnectionManager().shutdown(); } logger.info("billRecords response body = " + result); try { if (BODY_TYPE == BodyType.Type_JSON) { return jsonToMap(result); } else { return xmlToMap(result); } } catch (Exception e) { return getMyError("172003", ""); } }
From source file:com.baomidou.springwind.util.yuntongxin.CCPRestSDK.java
License:Open Source License
/** * ???//w w w . j av a 2 s. c o m * * @param verifyCode * ? ?????4-8? * @param to * ? ?? * @param displayNum * ?? ?????? * @param playTimes * ?? 1?31 * @param respUrl * ?? ??????Url???? * @param lang * ?? * @param userData * ?? ?? * @param welcomePrompt * ?? wav??????????verifyCodeplayVerifyCode * @param playVerifyCode * ?? wav????????verifyCode???playVerifyCode * @param maxCallTime * ?? ? * @return */ public HashMap<String, Object> voiceVerify(String verifyCode, String to, String displayNum, String playTimes, String respUrl, String lang, String userData, String welcomePrompt, String playVerifyCode, String maxCallTime) { HashMap<String, Object> validate = accountValidate(); if (validate != null) return validate; if ((isEmpty(verifyCode)) || (isEmpty(to))) throw new IllegalArgumentException("?:" + (isEmpty(verifyCode) ? " ?? " : "") + (isEmpty(to) ? " ?? " : "") + ""); CcopHttpClient chc = new CcopHttpClient(); DefaultHttpClient httpclient = null; try { httpclient = chc.registerSSL(SERVER_IP, "TLS", Integer.parseInt(SERVER_PORT), "https"); } catch (Exception e1) { e1.printStackTrace(); throw new RuntimeException("?httpclient" + e1.getMessage()); } String result = ""; try { HttpPost httppost = (HttpPost) getHttpRequestBase(1, VoiceVerify); String requsetbody = ""; if (BODY_TYPE == BodyType.Type_JSON) { JsonObject json = new JsonObject(); json.addProperty("appId", App_ID); json.addProperty("verifyCode", verifyCode); json.addProperty("to", to); if (!(isEmpty(displayNum))) json.addProperty("displayNum", displayNum); if (!(isEmpty(playTimes))) json.addProperty("playTimes", playTimes); if (!(isEmpty(respUrl))) json.addProperty("respUrl", respUrl); if (!(isEmpty(lang))) json.addProperty("lang", lang); if (!(isEmpty(userData))) json.addProperty("userData", userData); if (!(isEmpty(welcomePrompt))) json.addProperty("welcomePrompt", welcomePrompt); if (!(isEmpty(playVerifyCode))) json.addProperty("playVerifyCode", playVerifyCode); if (!(isEmpty(maxCallTime))) json.addProperty("maxCallTime", maxCallTime); requsetbody = json.toString(); } else { StringBuilder sb = new StringBuilder("<?xml version='1.0' encoding='utf-8'?><VoiceVerify>"); sb.append("<appId>").append(App_ID).append("</appId>").append("<verifyCode>").append(verifyCode) .append("</verifyCode>").append("<to>").append(to).append("</to>"); if (!(isEmpty(displayNum))) sb.append("<displayNum>").append(displayNum).append("</displayNum>"); if (!(isEmpty(playTimes))) sb.append("<playTimes>").append(playTimes).append("</playTimes>"); if (!(isEmpty(respUrl))) sb.append("<respUrl>").append(respUrl).append("</respUrl>"); if (!(isEmpty(lang))) sb.append("<lang>").append(lang).append("</lang>"); if (!(isEmpty(userData))) sb.append("<userData>").append(userData).append("</userData>"); if (!(isEmpty(welcomePrompt))) sb.append("<welcomePrompt>").append(welcomePrompt).append("</welcomePrompt>"); if (!(isEmpty(playVerifyCode))) sb.append("<playVerifyCode>").append(playVerifyCode).append("</playVerifyCode>"); if (!(isEmpty(maxCallTime))) sb.append("<maxCallTime>").append(maxCallTime).append("</maxCallTime>"); sb.append("</VoiceVerify>").toString(); requsetbody = sb.toString(); } logger.info("voiceVerify Request body = : " + requsetbody); System.out.println("" + requsetbody); BasicHttpEntity requestBody = new BasicHttpEntity(); requestBody.setContent(new ByteArrayInputStream(requsetbody.getBytes("UTF-8"))); requestBody.setContentLength(requsetbody.getBytes("UTF-8").length); httppost.setEntity(requestBody); HttpResponse response = httpclient.execute(httppost); status = response.getStatusLine().getStatusCode(); System.out.println("Https??" + status); HttpEntity entity = response.getEntity(); if (entity != null) result = EntityUtils.toString(entity, "UTF-8"); EntityUtils.consume(entity); } catch (IOException e) { e.printStackTrace(); logger.error(e.getMessage()); return getMyError("172001", ""); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); return getMyError("172002", ""); } finally { if (httpclient != null) httpclient.getConnectionManager().shutdown(); } logger.info("voiceVerify response body = " + result); try { if (BODY_TYPE == BodyType.Type_JSON) { return jsonToMap(result); } else { return xmlToMap(result); } } catch (Exception e) { return getMyError("172003", ""); } }
From source file:com.baomidou.springwind.util.yuntongxin.CCPRestSDK.java
License:Open Source License
/** * ??//from w w w . j ava 2 s .c o m * * @param to * ? ??? * @param mediaName * ?? ??? wavmediaTxt???mediaTxt * @param mediaTxt * ?? * @param displayNum * ?? ?????? * @param playTimes * ?? 1?31 * @param respUrl * ?? ????Url???? * @param userData * ?? ?? * @param txtSpeed * ?? ???-500500mediaTxt?0 * @param txtVolume * ?? ????-2020mediaTxt?0 * @param txtPitch * ?? ??-500500mediaTxt?0 * @param txtBgsound * ?? ?????6?16??0????mediaTxt? * @param playMode * ?? ?? , 0?? 1?0 * @return */ public HashMap<String, Object> landingCall(String to, String mediaName, String mediaTxt, String displayNum, String playTimes, String respUrl, String userData, String txtSpeed, String txtVolume, String txtPitch, String txtBgsound, String playMode) { HashMap<String, Object> validate = accountValidate(); if (validate != null) return validate; if (isEmpty(to)) throw new IllegalArgumentException("?:" + (isEmpty(to) ? " ??? " : "") + ""); if ((isEmpty(mediaName)) && (isEmpty(mediaTxt))) throw new IllegalArgumentException( "??????"); CcopHttpClient chc = new CcopHttpClient(); DefaultHttpClient httpclient = null; try { httpclient = chc.registerSSL(SERVER_IP, "TLS", Integer.parseInt(SERVER_PORT), "https"); } catch (Exception e1) { e1.printStackTrace(); throw new RuntimeException("?httpclient" + e1.getMessage()); } String result = ""; try { HttpPost httppost = (HttpPost) getHttpRequestBase(1, LandingCalls); String requsetbody = ""; if (BODY_TYPE == BodyType.Type_JSON) { JsonObject json = new JsonObject(); json.addProperty("appId", App_ID); json.addProperty("to", to); if (!(isEmpty(mediaName))) json.addProperty("mediaName", mediaName); if (!(isEmpty(mediaTxt))) json.addProperty("mediaTxt", mediaTxt); if (!(isEmpty(displayNum))) json.addProperty("displayNum", displayNum); if (!(isEmpty(playTimes))) json.addProperty("playTimes", playTimes); if (!(isEmpty(respUrl))) json.addProperty("respUrl", respUrl); if (!(isEmpty(userData))) json.addProperty("userData", userData); if (!(isEmpty(txtSpeed))) json.addProperty("txtSpeed", txtSpeed); if (!(isEmpty(txtVolume))) json.addProperty("txtVolume", txtVolume); if (!(isEmpty(txtPitch))) json.addProperty("txtPitch", txtPitch); if (!(isEmpty(txtBgsound))) json.addProperty("txtBgsound", txtBgsound); if (!(isEmpty(playMode))) json.addProperty("playMode", playMode); requsetbody = json.toString(); } else { StringBuilder sb = new StringBuilder("<?xml version='1.0' encoding='utf-8'?><LandingCall>"); sb.append("<appId>").append(App_ID).append("</appId>").append("<to>").append(to).append("</to>"); if (!(isEmpty(mediaName))) sb.append("<mediaName>").append(mediaName).append("</mediaName>"); else if (!(isEmpty(mediaName))) sb.append("<mediaName>").append(mediaName).append("</mediaName>"); if (!(isEmpty(mediaTxt))) sb.append("<mediaTxt>").append(mediaTxt).append("</mediaTxt>"); if (!(isEmpty(displayNum))) sb.append("<displayNum>").append(displayNum).append("</displayNum>"); if (!(isEmpty(playTimes))) sb.append("<playTimes>").append(playTimes).append("</playTimes>"); if (!(isEmpty(respUrl))) sb.append("<respUrl>").append(respUrl).append("</respUrl>"); if (!(isEmpty(userData))) sb.append("<userData>").append(userData).append("</userData>"); if (!(isEmpty(txtSpeed))) sb.append("<txtSpeed>").append(txtSpeed).append("</txtSpeed>"); if (!(isEmpty(txtVolume))) sb.append("<txtVolume>").append(txtVolume).append("</txtVolume>"); if (!(isEmpty(txtPitch))) sb.append("<txtPitch>").append(txtPitch).append("</txtPitch>"); if (!(isEmpty(txtBgsound))) sb.append("<txtBgsound>").append(txtBgsound).append("</txtBgsound>"); if (!(isEmpty(playMode))) sb.append("<playMode>").append(playMode).append("</playMode>"); sb.append("</LandingCall>").toString(); requsetbody = sb.toString(); } logger.info("landingCalls Request body = : " + requsetbody); System.out.println("" + requsetbody); BasicHttpEntity requestBody = new BasicHttpEntity(); requestBody.setContent(new ByteArrayInputStream(requsetbody.getBytes("UTF-8"))); requestBody.setContentLength(requsetbody.getBytes("UTF-8").length); httppost.setEntity(requestBody); HttpResponse response = httpclient.execute(httppost); status = response.getStatusLine().getStatusCode(); System.out.println("Https??" + status); HttpEntity entity = response.getEntity(); if (entity != null) result = EntityUtils.toString(entity, "UTF-8"); EntityUtils.consume(entity); } catch (IOException e) { e.printStackTrace(); logger.error(e.getMessage()); return getMyError("172001", ""); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); return getMyError("172002", ""); } finally { if (httpclient != null) httpclient.getConnectionManager().shutdown(); } logger.info("landingCall response body = " + result); try { if (BODY_TYPE == BodyType.Type_JSON) { return jsonToMap(result); } else { return xmlToMap(result); } } catch (Exception e) { return getMyError("172003", ""); } }
From source file:com.baomidou.springwind.util.yuntongxin.CCPRestSDK.java
License:Open Source License
/** * ???/* w w w . jav a2 s . com*/ * * @param to * ? ??????????100 * @param templateId * ? ?Id * @param datas * ?? ???{??} * @return */ public HashMap<String, Object> sendTemplateSMS(String to, String templateId, String[] datas) { HashMap<String, Object> validate = accountValidate(); if (validate != null) return validate; if ((isEmpty(to)) || (isEmpty(App_ID)) || (isEmpty(templateId))) throw new IllegalArgumentException("?:" + (isEmpty(to) ? " ?? " : "") + (isEmpty(templateId) ? " ?Id " : "") + ""); CcopHttpClient chc = new CcopHttpClient(); DefaultHttpClient httpclient = null; try { httpclient = chc.registerSSL(SERVER_IP, "TLS", Integer.parseInt(SERVER_PORT), "https"); } catch (Exception e1) { e1.printStackTrace(); throw new RuntimeException("?httpclient" + e1.getMessage()); } String result = ""; try { HttpPost httppost = (HttpPost) getHttpRequestBase(1, TemplateSMS); String requsetbody = ""; if (BODY_TYPE == BodyType.Type_JSON) { JsonObject json = new JsonObject(); json.addProperty("appId", App_ID); json.addProperty("to", to); json.addProperty("templateId", templateId); if (datas != null) { StringBuilder sb = new StringBuilder("["); for (String s : datas) { sb.append("\"" + s + "\"" + ","); } sb.replace(sb.length() - 1, sb.length(), "]"); JsonParser parser = new JsonParser(); JsonArray Jarray = parser.parse(sb.toString()).getAsJsonArray(); json.add("datas", Jarray); } requsetbody = json.toString(); } else { StringBuilder sb = new StringBuilder("<?xml version='1.0' encoding='utf-8'?><TemplateSMS>"); sb.append("<appId>").append(App_ID).append("</appId>").append("<to>").append(to).append("</to>") .append("<templateId>").append(templateId).append("</templateId>"); if (datas != null) { sb.append("<datas>"); for (String s : datas) { sb.append("<data>").append(s).append("</data>"); } sb.append("</datas>"); } sb.append("</TemplateSMS>").toString(); requsetbody = sb.toString(); } //? System.out.println("" + requsetbody); logger.info("sendTemplateSMS Request body = " + requsetbody); BasicHttpEntity requestBody = new BasicHttpEntity(); requestBody.setContent(new ByteArrayInputStream(requsetbody.getBytes("UTF-8"))); requestBody.setContentLength(requsetbody.getBytes("UTF-8").length); httppost.setEntity(requestBody); HttpResponse response = httpclient.execute(httppost); //??? status = response.getStatusLine().getStatusCode(); System.out.println("Https??" + status); HttpEntity entity = response.getEntity(); if (entity != null) result = EntityUtils.toString(entity, "UTF-8"); EntityUtils.consume(entity); } catch (IOException e) { e.printStackTrace(); logger.error(e.getMessage()); return getMyError("172001", "" + "Https?" + status); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); return getMyError("172002", ""); } finally { if (httpclient != null) httpclient.getConnectionManager().shutdown(); } logger.info("sendTemplateSMS response body = " + result); try { if (BODY_TYPE == BodyType.Type_JSON) { return jsonToMap(result); } else { return xmlToMap(result); } } catch (Exception e) { return getMyError("172003", ""); } }
From source file:com.baomidou.springwind.util.yuntongxin.CCPRestSDK.java
License:Open Source License
/** * ?????/*ww w . j a v a2 s . c o m*/ * * @param appId * ? Id * @param friendlyName * ? ????? * @return */ public HashMap<String, Object> querySubAccount(String friendlyName) { HashMap<String, Object> validate = accountValidate(); if (validate != null) return validate; if ((isEmpty(friendlyName))) { logger.debug("?: ????? "); throw new IllegalArgumentException("?: ????? "); } CcopHttpClient chc = new CcopHttpClient(); DefaultHttpClient httpclient = null; try { httpclient = chc.registerSSL(SERVER_IP, "TLS", Integer.parseInt(SERVER_PORT), "https"); } catch (Exception e1) { e1.printStackTrace(); throw new RuntimeException("?httpclient" + e1.getMessage()); } String result = ""; try { HttpPost httppost = (HttpPost) getHttpRequestBase(1, Query_SubAccountByName); String requsetbody = ""; if (BODY_TYPE == BodyType.Type_JSON) { JsonObject json = new JsonObject(); json.addProperty("appId", App_ID); json.addProperty("friendlyName", friendlyName); requsetbody = json.toString(); } else { requsetbody = "<?xml version='1.0' encoding='utf-8'?><SubAccount>" + "<appId>" + App_ID + "</appId>" + "<friendlyName>" + friendlyName + "</friendlyName>" + "</SubAccount>"; } logger.info("querySubAccountByName Request body = " + requsetbody); System.out.println("" + requsetbody); BasicHttpEntity requestBody = new BasicHttpEntity(); requestBody.setContent(new ByteArrayInputStream(requsetbody.getBytes("UTF-8"))); requestBody.setContentLength(requsetbody.getBytes("UTF-8").length); httppost.setEntity(requestBody); HttpResponse response = httpclient.execute(httppost); status = response.getStatusLine().getStatusCode(); System.out.println("Https??" + status); HttpEntity entity = response.getEntity(); if (entity != null) result = EntityUtils.toString(entity, "UTF-8"); EntityUtils.consume(entity); } catch (IOException e) { e.printStackTrace(); logger.error(e.getMessage()); return getMyError("172001", ""); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); return getMyError("172002", ""); } finally { if (httpclient != null) httpclient.getConnectionManager().shutdown(); } logger.info("querySubAccount result " + result); try { if (BODY_TYPE == BodyType.Type_JSON) { return jsonToMap(result); } else { return xmlToMap(result); } } catch (Exception e) { return getMyError("172003", ""); } }