2

Unable to print complete JSON Results. My Program as follows:

Am new to this program. Unable to find the reason for the error.

Query for the program as follows:

curl -X POST -H 
"Content-Type:application/json" --header 
'X-Auth-Token:IEkmVGHsa4R3cGPw56MkfQ' -d '{
"sensor_key":"e4aa3e35t675fc57ce81f3dd6e2dcdef492at4f7", 
"date_ranges":[{
"from":"2015/04/02 17:05:00", 
"to":"2015/04/02 17:10:00"
}], 
"time_zone":"Mumbai" , 
"time_format":"str", 
"per":"50"
}' 
'https://api.datonis.io/api/v2/datonis_query/sensor_event_raw_data


    package IOT;

    import java.util.HashMap;
    import java.util.Map;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicHeader;
    import org.apache.http.protocol.HTTP;
    import org.json.JSONObject;
    import org.json.JSONArray;

    public class HttpPostWithBody {

    public static void main(String args[]) {
    String Message = "6f2159f998";

    try {
        new HttpPostWithBody().sendJSONData(Message);
    } catch (Exception E) {
    System.out.println("Exception Occured. " + E.getMessage());
    }
    }

    public String sendJSONData(String message) throws Exception {

    //creating map object to create JSON object from it
    Map< String, Object >jsonValues = new HashMap< String, Object >();
    jsonValues.put("sensor_key",message);
    jsonValues.put("from", "2016/08/29 16:55:00");
    jsonValues.put("to", "2016/08/29 17:05:00");
    jsonValues.put("time_zone", "Mumbai");
    jsonValues.put("per", "50");
    jsonValues.put("metrics", "1st data");

    JSONObject json = new JSONObject(jsonValues);


    String url = "https://api.datonis.io/api/v2/datonis_query/sensor_event_raw_data";

    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    post.setHeader("Content-Type", "application/json");
    post.setHeader("Accept", "application/json"); 
    post.setHeader("X-Auth-Token", "9v8IjBku0a9y-D7SpLq6ZA");

    //setting json object to post request.
    StringEntity entity = new StringEntity(json.toString(), "UTF8");
    entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

    post.setEntity(entity);
    //this is your response:

    HttpResponse response = client.execute(post);

    //JSONObject myObject1 = new JSONObject(response);

    JSONArray ja = new JSONArray(response);

    //JSONObject jo = ja.getJSONObject();
    System.out.println("Response: " + ja.getJSONObject(0));

    System.out.println("Response: " + response.getStatusLine());
    return response.getStatusLine().toString();
    }
    }
cyberlobe
  • 1,783
  • 1
  • 18
  • 30
  • It is not helpfull to other users to post questions without a full problem description. What is not working? What error do you get? – tak3shi Sep 09 '16 at 12:31
  • Your Answer is really helpful. Am new to this forum. –  Sep 09 '16 at 12:48

2 Answers2

2

Try this:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
...
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(json.toString());
String prettyJsonString = gson.toJson(je);
System.out.println(prettyJsonString);

Output:

{
  "sensor_key": "6f2159f998",
  "from": "2016/08/29 16:55:00",
  "to": "2016/08/29 17:05:00",
  "metrics": "1st data",
  "time_zone": "Mumbai",
  "per": "50"
}

enter image description here

See the whole code, here

For more info about GSON library, check this

To download GSON, check this

Jad Chahine
  • 6,849
  • 8
  • 37
  • 59
  • I couldn't understand this. Can you please help me in write / change my program ans post the same. –  Sep 09 '16 at 12:25
  • 1
    @GaneshRavikumar Stack Overflow is not a coding writing service. – cassiomolin Sep 09 '16 at 12:27
  • @GaneshRavikumar: I already put a link that takes you to the whole code in my answer, [check it](http://collabedit.com/q4n8x) – Jad Chahine Sep 09 '16 at 12:28
  • am getting the results which post the actual result will be as follows: Returns upto 50 events. { "e4aa3e35t675fc57ce81f3dd6e2dcdef492at4f7": { "total_event_count": 30, "page_number": 1, "event_data": [ { "data": { "mem": 208, "cpu": 1.3250000000000002 }, "time_stamp": "2015/04/02 17:09:59" }, { "data": { "mem": 219, "cpu": 1.45 –  Sep 09 '16 at 12:36
  • Can you help me on this pls the another error as follows: Exception Occured. JSONArray initial value should be a string or collection or array. The recent AUTHENDICATION KEY IS: SOX_0P37BCqoigWZ-gcC5w –  Sep 09 '16 at 12:55
  • @GaneshRavikumar: I think that you should ask another question related to this error and I or other people will help you, and it seems that this answer is enough to your question and you should close the question by accepting the best answer. Thanks. – Jad Chahine Sep 15 '16 at 06:36
0

Probabbly, you should use the same solution: Pretty-Print JSON in Java

Also, GSON is quite good overall

Community
  • 1
  • 1