gpt4 book ai didi

java - Java 中的 JSON header 数组

转载 作者:太空宇宙 更新时间:2023-11-04 12:16:14 31 4
gpt4 key购买 nike

我需要发布数据来获取 IOT 结果:

我收到的错误如下:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: JSONObject cannot be resolved to a type JSONObject cannot be resolved to a type at IOT.httpPostWithBody.sendJSONData(httpPostWithBody.java:40) at IOT.httpPostWithBody.main(httpPostWithBody.java:27)

我的JAVA程序如下:

package IOT;

//package com.test.Second;

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;


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("thing_key",message);
jsonValues.put("from", "2016/08/29 16:55:00");
jsonValues.put("to", "2016/08/29 17:05:00");
jsonValues.put("time_zone", "Asia/Calcutta");
jsonValues.put("per", "200");
jsonValues.put("metrics", "1st data");

JSONObject json = new JSONObject(jsonValues);


String url = "https://api.datonis.io/api/v3/datonis_query/thing_data";
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setHeader("Content-Type", "application/json");
post.setHeader("X-Auth-Token", "TtJd3Qb9eq5xqPfK2zabXg");
//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);
System.out.println("Response: " + response.getStatusLine());
return response.getStatusLine().toString();
}
}

最佳答案

问题中的 URL 有一个错误,现在已解决。

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;


public class HttpPostWithBody {

public static void main(String args[]) {
String Message = "62159f998";

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/thing_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);
System.out.println("Response: " + response.getStatusLine());
return response.getStatusLine().toString();
}
}

关于java - Java 中的 JSON header 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39385139/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com