eclipse - How can I retrieve a feed in JSON from a Java Servlet? -
I want to make a HTTP request and want to store the result in JSONObject. I have not done much work with servlets, so I am unsure what I am 1) request to be done properly, and 2) JSONObject should be made. I've imported JSONObject and JSONArray classes, but I do not know how I should use them. Here's what I have:
throws public zero doGet (httpPervateate request), IOException {// Try the URL {// with a single string. URL url = new URL (FEED_URL); // Server BufferedReader = Read all the text returned by the new BufferedReader (New InputStreamReader (url.openstream ()); String; While ((str = in.readLine ())! = Null {// str A line of text ; ReadLine () character of new line (s)} in.close () strips;} hold (malformedLELException e) {} catcher (IOException e) {}
My FEED_URL already It has been written so that it will return the formatted feed for JSON.
This is getting me for hours Thank you very much, you guys are a valuable resource!
Collect responses in the first string:
BufferedReader = in the new BufferedReader (New InputStreamReader (url.openstream ()); StringBuller FullSpons = New StringBuilder (); Letter string; while {(str = in.readLine ()) = null {fullResponse.append (str);}
After that, if the string starts with "{" You can use it:
JSONObject obj = new JSONObject (fullResponse.toString ()); // [1]
And if it starts with "[", then you can use it:
JSONArray arr = New JSONArray (FullResponse.toStrin ()); // [2]
[1]
[2]
Comments
Post a Comment