private void loadJockeyData() { File file = new File(this.getDataFolder(), "data.json"); if (!file.exists()) { try { file.createNewFile(); InputStream inStream = getResource("data.json"); OutputStream out = new FileOutputStream(file); ByteStreams.copy(inStream, out); close(inStream); close(out); } catch (IOException ex) { getLogger().log(Level.SEVERE, "Could not load jockey data.", ex); return; } } try { Gson gson = new Gson(); jockeyDataManager = gson.fromJson(new FileReader(file), JockeyDataManager.class); } catch (Exception e) { getLogger().log(Level.SEVERE, "Could not load jockey data.", e); } }
private String search(String channel, String sender, String login, String hostname, String message, String args[], String statement) throws Exception { Gui.log(new String(new Gson().toJson(new Request(statement, sender)) .getBytes(), "UTF-8")); return PostServer .SendPost( new String(""), new String( ("https://en.wikipedia.org/w/api.php?action=opensearch&search=" + statement + "&limit=1&namespace=0&format=json") .getBytes(), "UTF-8")); }
private String toRobot(String channel, String sender, String login, String hostname, String message, String args[], String statement) throws Exception { // StringEntity postingString = new StringEntity(new String(new Gson() // .toJson(new Request(statement, sender)).getBytes(), "UTF-8")); Gui.log(new String(new Gson().toJson(new Request(statement, sender)) .getBytes(), "UTF-8")); return PostServer.SendPost( new String(new Gson().toJson(new Request(statement, sender)) .getBytes(), "UTF-8"), "http://www.tuling123.com/openapi/api").split("\"")[5]; /* * HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost * post = new HttpPost("http://www.tuling123.com/openapi/api"); * post.setEntity(postingString); post.setHeader("Content-type", * "application/json"); post.addHeader("Content-Type", "charset=UTF-8"); * * HttpResponse res = httpClient.execute(post); JSONObject jsonObject = * new JSONObject(); if (res != null) { jsonObject = new * JSONObject(IOUtils.toString(res.getEntity() .getContent(), "UTF-8")); * String text = jsonObject.get("text").toString(); if (text != null) { * // success System.out.println(jsonObject.getString("text")); } else { * throw new Exception("Error #" + jsonObject.get("code") + " " + * jsonObject.get("error_message")); } } return jsonObject.toString(); */ // return in.readLine().split("\"")[5]; /* * final URL url = new URL("http://www.tuling123.com/openapi/api"); * final URLConnection urlConnection = url.openConnection(); * urlConnection.setDoOutput(true); * urlConnection.setRequestProperty("Content-Type", * "application/json; charset=utf-8"); urlConnection.connect(); final * OutputStream outputStream = urlConnection.getOutputStream(); * outputStream.write(("{\"key\": \"" + * "3aab1c245bb8401fa8a3b3bf1689bc35" + "\"}").getBytes("UTF-8")); * outputStream.write(("{\"info\": \"" + statement + "\"}") * .getBytes("UTF-8")); outputStream.write(("{\"userid\": \"" + sender + * "\"}") .getBytes("UTF-8")); outputStream.flush(); BufferedReader in = * new BufferedReader(new InputStreamReader( * urlConnection.getInputStream())); return * in.readLine().split("\"")[5]; */ /* * String urlString = "http://www.tuling123.com/openapi/api"; HttpClient * httpClient = HttpClientBuilder.create().build(); HttpPost postRequest * = new HttpPost(urlString); * * // build request parameters StringBody apiKey = new * StringBody("3aab1c245bb8401fa8a3b3bf1689bc35", * ContentType.MULTIPART_FORM_DATA); StringBody info = new * StringBody(statement, ContentType.MULTIPART_FORM_DATA); StringBody * userid = new StringBody(sender, ContentType.MULTIPART_FORM_DATA); * * MultipartEntityBuilder builder = MultipartEntityBuilder.create(); * builder.addPart("key", apiKey); builder.addPart("info", info); // * builder.addPart("userid", userid); * * postRequest.setEntity(builder.build()); HttpResponse res = null; * * // execute the request res = httpClient.execute(postRequest); * * JSONObject jsonObject = new JSONObject(); if (res != null) { * jsonObject = new JSONObject(IOUtils.toString(res.getEntity() * .getContent(), "UTF-8")); String text = * jsonObject.get("text").toString(); if (text != null) { // success * System.out.println(jsonObject.getString("text")); } else { throw new * Exception("Error #" + jsonObject.get("code") + " " + * jsonObject.get("error_message")); } } return jsonObject.toString(); */ }
public Gson getGson() { return gson; }
/** * Gets gson. * * @return Value of gson. */ public Gson getGson() { return gson; }
/** * Returns the object as a Json String. * * @param gson the gson object to use to convert it to a Json string. * @return the Json String */ public String toJSON(Gson gson) { return gson.toJson(this, Region.class); }