HTTPResponse as JSON in Java

I tried to get JSONObject from an HTTP response.

try { GetMethod postMethod = new GetMethod(); postMethod.setURI(new URI(url, true)); postMethod.setRequestHeader("Accept", "application/json"); httpClient.executeMethod(postMethod); String resp=postMethod.getResponseBodyAsString(); org.json.JSONTokener tokener = new org.json.JSONTokener(resp); finalResult = new org.json.JSONArray(tokener); return finalResult; } 

But I got a warning at runtime since

Transition to the response body of a buffer of large or unknown size. Using getResponseBodyAsStream recommended.

Should I get the answer as a thread suggested by the JVM? If so, how can I parse JSON?

+6
source share
3 answers

if you want to send jsonObjects from the server (tomcat server)

For server -

jsonObjects creation -

I called toJson() to create jsonObjects , this is an implementation -

 final JSONObject arr = new JSONObject(); for (int i = 0; i < contactStatus.size(); i++) { ContactStatus contactObject = contactStatus.get(i); try { arr.put(String.valueOf(i), toJson(value1, value2,, value3)); } catch (JSONException e) { catch block e.printStackTrace(); } } //Here we serialize the stream to a String. final String output = arr.toString(); response.setContentLength(output.length()); out.print(output);//out is object of servlet output stream. public static Object toJsonForContact(String value1, boolean value2, double value3) throws JSONException { JSONObject contactObject = new JSONObject(); contactObject.put("id", id); contactObject.put("status", value1); contactObject.put("distance", value2); contactObject.put("relation", value3); return contactObject; } 

so that your jsonObjects ready to send, we write these objects to ServletoutputStream .

on the client side -

 while ((ReadResponses = in.readLine()) != null) { Constants.Response_From_server = ReadResponses; if (Constants.Response_From_server.startsWith("{")) { ListOfContactStatus = new ArrayList<ContactStatus>(); ContactStatus contactStatusObject; try { JSONObject json = new JSONObject(Constants.Response_From_server); for (int i = 0; i < json.length(); i++) { contactStatusObject = new ContactStatus(); JSONObject json1 = json.getJSONObject(String.valueOf(i)); System.out.println("" + json1.getString("id")); System.out.println("" + json1.getBoolean("status")); System.out.println("" + json1.getDouble("distance")); contactStatusObject.setId(json1.getString("id")); contactStatusObject.setStatus(json1.getBoolean("status")); contactStatusObject.setDistance((float) json1.getDouble("distance")); ListOfContactStatus.add(contactStatusObject); System.out.println("HTTPTransport:sendMessage Size of ListOfContactStatus" + ListOfContactStatus.size()); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } 
+1
source

Is your server installed to inform clients of how large its responses are? If not, your server sends data streams, and it is technically impossible to determine how much buffer space is required to respond to it, which warns that something potentially dangerous is happening.

+3
source

You can easily create a JSonObject usin Java EE 7. Sample code.

  JsonReader reader = Json.createReader(new URI(url, true)); JsonObject jsonObject=reader.readObject(); 

For more information, click here. http://docs.oracle.com/javaee/7/tutorial/doc/jsonp003.htm#BABHAHIA

+1
source

Source: https://habr.com/ru/post/947652/


All Articles