Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java发送JSON消息_Java_Json_Http Post - Fatal编程技术网

使用Java发送JSON消息

使用Java发送JSON消息,java,json,http-post,Java,Json,Http Post,我正在尝试发送此json消息: {“ruido_外部”:0,“co2_外部”:0,“humedad_内部”:0,“ruido_外部”:0,“co_内部”:0,“co_内部”:0,“co_外部”:0,“temperatura_内部”:0,“pm_25”:8,“pm_10”:10,“humedad_外部”:0,“时间戳”:14949780840000} 代码 public static void sendRequestPostRenam(JSONObject json) throws IOExcep

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

{“ruido_外部”:0,“co2_外部”:0,“humedad_内部”:0,“ruido_外部”:0,“co_内部”:0,“co_内部”:0,“co_外部”:0,“temperatura_内部”:0,“pm_25”:8,“pm_10”:10,“humedad_外部”:0,“时间戳”:14949780840000}

代码

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();
        }
    }
public static void sendRequestPostRename(JSONObject json)抛出IOException、JSONException{
CloseableHttpClient httpClient=HttpClientBuilder.create().build();
试一试{
URIBuilder=new URIBuilder(Config.urlJSON).addParameter(“访问令牌”,Config.renamToken);
HttpPost请求=新建HttpPost(builder.build());
StringEntity参数=新的StringEntity(json.toString());
请求.setEntity(参数);
CloseableHttpResponse response=httpClient.execute(请求);
//
字符串内容=EntityUtils.toString(response.getEntity());
Log.debug(内容);
//
int statusCode=response.getStatusLine().getStatusCode();
Log.debug(“[状态:+statusCode+]”);
}捕获(例外情况除外){
Log.debug(例如toString());
}最后{
httpClient.close();
}
}
内容的输出为:

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

消息说“时间戳不能为空”,但在变量“参数””中包含时间戳值。请求对象似乎没有这个json值

编辑1:

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

curl-X POST-H'内容类型:application/json'-d'{“pm25”:35,“时间戳”:147805158}{yoq3ugqdqdkp4d1l3y6xiyp-Lb6fyvavpF3Lm-8cD}

最后一个命令的结果是:

{“状态”:“确定”,“信息”:[],“时间戳”:14950722199}


非常感谢您提供的任何帮助。

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

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

无论如何,您可以尝试在请求HttpPost对象中添加内容类型的头。
request.addHeader(HttpHeaders.CONTENT\u TYPE,“application/json”)

我还不能对你的帖子发表评论,所以将查询粘贴到这里作为答案

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

无论如何,您可以尝试在请求HttpPost对象中添加内容类型的头。
request.addHeader(HttpHeaders.CONTENT\u TYPE,“application/json”)

感谢您的评论。我必须向API发送一些数据。如果我使用curl,我会得到一个状态:ok回答。这是comand:curl-xpost-H'Content-Type:application/json'-d'{“pm25”:35,“timestamp”:147805158}{yoq3ugqdkp4d1l3y6xiyp-Lb6fyvavpF3Lm-8cD}是的,我使用了头,但现在我得到一个内部服务器错误500。现在我将使用标题来模拟curl消息,但它看起来根本不起作用,但我找不到为什么要感谢您的评论。我必须向API发送一些数据。如果我使用curl,我会得到一个状态:ok回答。这是comand:curl-xpost-H'Content-Type:application/json'-d'{“pm25”:35,“timestamp”:147805158}{yoq3ugqdkp4d1l3y6xiyp-Lb6fyvavpF3Lm-8cD}是的,我使用了头,但现在我得到一个内部服务器错误500。现在我将使用头来模拟curl消息,但看起来根本不起作用,但我找不到原因