gpt4 book ai didi

java - 使用 Java 发送 JSON 消息

转载 作者:太空宇宙 更新时间:2023-11-04 11:28:50 25 4
gpt4 key购买 nike

我正在尝试发送此 json 消息:

{"ruido_exterior":0,"co2_exterior":0,"humedad_interior":0,"ruido_interior":0,"temperatura_exterior":0,"co_interior":0,"co2_interior":0,"co_exterior":0,"temperatura_interior":0,"pm_25":8,"pm_10":10,"humedad_exterior":0,"timestamp":1494978084000}

代码

public static void sendRequestPostRenam(JSONObject json) throws IOException, JSONException {

CloseableHttpClient httpClient = HttpClientBuilder.create().build();

try {

URIBuilder builder = new URIBuilder(Config.urlJSON).addParameter("access-token", Config.renamToken);

HttpPost request = new HttpPost(builder.build());
StringEntity params = new StringEntity(json.toString());

request.setEntity(params);

CloseableHttpResponse response = httpClient.execute(request);

//<editor-fold defaultstate="collapsed" desc="PRUBEAS DEBUG">
String content = EntityUtils.toString(response.getEntity());
Log.debug(content);
//</editor-fold>

int statusCode = response.getStatusLine().getStatusCode();

Log.debug("[STATUS:" + statusCode + "]");

} catch (Exception ex) {
Log.debug(ex.toString());
} finally {
httpClient.close();
}
}

内容的输出为:

{"status":"error","info":{"timestamp":["Timestamp no puede estar vacío."]},"timestamp":1495068046}

消息显示“时间戳不能为空”,但在变量“params”中包含时间戳值。看起来请求对象没有这个 json 值。

编辑1:

我将解释我需要做什么。我必须通过 Json 向 API 发送一些数据。它具有访问 token 身份验证,如果我使用curl,发送数据不会有任何问题:

curl -X POST -H 'Content-Type: application/json' -d '{"pm25": 35, "timestamp": 147805158}' https://api.com/data/insert?access-token={Yoq3UGQqDKP4D1L3Y6xIYp-Lb6fyvavpF3Lm-8cD}

最后一个命令的结果是:

{"status":"ok","info":[],"timestamp":1495072199}

任何帮助都会非常感激。

最佳答案

我还无法对您的帖子发表评论,因此将查询粘贴到此处作为答案。

如果我没有错过任何内容,那么您正在打印的内容取决于您正在调用的远程服务中定义的处理和业务逻辑。您应该提供有关远程服务的更多详细信息。

无论如何,您可以尝试在请求 HttpPost 对象中添加内容类型的 header 。 request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json")

关于java - 使用 Java 发送 JSON 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44036986/

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