java - 如何在java中打印完整的json http请求结果

标签 java json httprequest

无法打印完整的 JSON 结果。我的程序如下:

我是这个程序的新手。无法找到错误原因。

查询程序如下:

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();
    }
    }

最佳答案

试试这个:

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);

输出:

{
  "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

查看整个代码, here

有关 GSON 库的更多信息,请查看 this

要下载 GSON,请选中 this

关于java - 如何在java中打印完整的json http请求结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39410848/

相关文章:

java - 什么是最好的软件开发分析和设计工具,能够将设计转化为实际代码?

ruby-on-rails - 使用关联数据发布 JSON

c# - 如何关闭从方法调用返回的 HttpWebResponse

javascript - 使用变量作为多级 json 对象中的键

java - 如何告诉 jackson 在反序列化期间忽略空对象?

node.js - 如何发送 readstream 作为响应

php - 将数组从 PHP 发送到 GWT 的问题

data-structures - 来自现有集合与迭代的 Java ArrayDeque 实例化

java - Keycloak 强制刷新 token

java - 将 SOAP header 添加到 spring-ws 2.2.0 端点响应