如何在java中打印完整的json http请求结果
无法打印完整的JSON结果。我的计划如下:如何在java中打印完整的json http请求结果,java,json,httprequest,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
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();
}
}
我是这个项目的新手。找不到错误的原因
按如下方式查询程序:
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();
}
}
curl-X POST-H
“内容类型:应用程序/json”--标题
'X-Auth-Token:IEkmVGHsa4R3cGPw56MkfQ'-d'{
“传感器键”:“e4aa3e35t675fc57ce81f3dd6e2dcdef492at4f7”,
“日期范围”:[{
“发件人”:“2015/04/02 17:05:00”,
“收件人”:“2015/04/02 17:10:00”
}],
“时区”:“孟买”,
“时间格式”:“str”,
“per”:“50”
}'
'https://api.datonis.io/api/v2/datonis_query/sensor_event_raw_data
包装物联网;
导入java.util.HashMap;
导入java.util.Map;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.entity.StringEntity;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicHeader;
导入org.apache.http.protocol.http;
导入org.json.JSONObject;
导入org.json.JSONArray;
公共类HttpPostWithBody{
公共静态void main(字符串参数[]){
字符串消息=“6f2159f998”;
试一试{
新建HttpPostWithBody().sendJSONData(消息);
}捕获(例外E){
System.out.println(“发生异常。”+E.getMessage());
}
}
公共字符串sendJSONData(字符串消息)引发异常{
//创建映射对象以从中创建JSON对象
MapjsonValues=newhashmap();
jsonValues.put(“传感器按键”,消息);
jsonValues.put(“自”,“2016/08/29 16:55:00”);
jsonValues.put(“至”,“2016/08/29 17:05:00”);
jsonValues.put(“时区”、“孟买”);
jsonValues.put(“per”,“50”);
jsonValues.put(“度量”、“第一数据”);
JSONObject json=新的JSONObject(jsonValues);
字符串url=”https://api.datonis.io/api/v2/datonis_query/sensor_event_raw_data";
DefaultHttpClient=新的DefaultHttpClient();
HttpPost=新的HttpPost(url);
setHeader(“内容类型”、“应用程序/json”);
post.setHeader(“接受”、“应用程序/json”);
post.setHeader(“X-Auth-Token”,“9v8IjBku0a9y-D7SpLq6ZA”);
//将json对象设置为post请求。
StringEntity=新的StringEntity(json.toString(),“UTF8”);
setContentType(新的BasicHeader(HTTP.CONTENT_TYPE,“application/json”);
后设实体(实体);
//这是你的回答:
HttpResponse response=client.execute(post);
//JSONObject MyObject 1=新JSONObject(响应);
JSONArray ja=新JSONArray(响应);
//JSONObject jo=ja.getJSONObject();
System.out.println(“响应:+ja.getJSONObject(0));
System.out.println(“响应:+Response.getStatusLine());
返回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"
}
查看整个代码,
有关GSON库的更多信息,请查看
要下载GSON,请检查,我无法理解这一点。你能帮我写/更改我的程序吗?@GaneshRavikumar Stack Overflow不是一个编码写入服务。@GaneshRavikumar:我已经在我的答案中添加了一个链接,将你带到整个代码,我得到的结果显示实际结果如下:返回多达50个事件。{“e4aa3e35t675fc57ce81f3dd6e2dcdef492at4f7:{“事件总数”:30,“页码”:1,“事件数据”:[{“数据”:{“内存”:208,“cpu”:1.3250000002},“时间戳”:“2015/04/02 17:09:59”},{“数据”:{“内存”:219,“cpu”:1.45你能帮我解决这个问题吗?另一个错误如下:发生异常。JSONArray初始值应该是字符串、集合或数组。最近的身份验证密钥是:SOX_0p37CQOIGWZ-gcC5wIt对其他用户发布没有完整问题描述的问题没有帮助。什么不起作用?你得到了什么错误?你的答案真的很有帮助。我是这个论坛的新手。