testymain 0 Posted November 19, 2018 I have built an REST API and i want to read from it. My JsonReader class looks like this import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.nio.charset.Charset; import org.json.JSONException; import org.json.JSONObject; public class JsonReader { private static String readAll(Reader rd) throws IOException { StringBuilder sb = new StringBuilder(); int cp; while ((cp = rd.read()) != -1) { sb.append((char) cp); } return sb.toString(); } public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException { InputStream is = new URL(url).openStream(); try { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JSONObject json = new JSONObject(jsonText); return json; } finally { is.close(); } } public static String getTask() { JSONObject json = null; try { json = readJsonFromUrl(""); } catch (JSONException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return json.toString(); } } this standalone works great, but as soon as i call JsonReader.getTask() in my script 2 tings happens. Either the script doesnt even start when i click start from local scripts / script starts but does nothing, no error in log or anything. did anyone encounter it? Is there any other method how to read from my API?
jarred788 9 Posted November 19, 2018 Im not too sure but i think it has something to do with the json library. You need to include it with compiled jar script. Again im not too sure.
testymain 0 Author Posted November 19, 2018 Yea i was looking for solution and what i found is basically the same what you said. The libraries arent compiled with script to jar, but i don't know how do i achieve this?
jarred788 9 Posted November 19, 2018 Maybe copy the lib folder from your project into dreambot scripts folder?
testymain 0 Author Posted November 19, 2018 Maybe i am doing it wrong, but what do you mean by lib foler, i used build path and added external jar. i moved this jar to dreambot where is client.jar, built path again but it didnt help. As i said, idk what you mean by lob folder, maybe i have to decompile that jar and export that package with classes? Edit: I also tried exporting it as Runnable jar because it has option to packge all dependant libraries to jar, but that didnt work either, script just doesnt start
testymain 0 Author Posted November 19, 2018 Maybe i cant use 3rd party libraries? Does anyone solved how to read from api?
jarred788 9 Posted November 19, 2018 Maybe try this https://stackoverflow.com/questions/6467848/how-to-get-http-response-code-for-a-url-in-java Just get the response from ur api and construct the objects yourself. Just a thought.
testymain 0 Author Posted November 19, 2018 I thought about not using 3rd part libs, but i actually solved it. So if anyone will look for it, i found all classes on https://github.com/stleary/JSON-java and just pasted it to my project, and now it works like a charm
Recommended Posts
Archived
This topic is now archived and is closed to further replies.