codenameone在POST中发送JSON正文

codenameone在POST中发送JSON正文,codenameone,Codenameone,我尝试使用Codename One发送带有JSON正文的POST请求。 下面是建立连接并发送消息的代码 ByteArrayOutputStream bOutput = new ByteArrayOutputStream(); bOutput.write("{\"Key1\": \"Value1\",\"Key2\": \"Value2\"}".getBytes()); try { connectAction connReq = new connect

我尝试使用Codename One发送带有JSON正文的POST请求。 下面是建立连接并发送消息的代码

    ByteArrayOutputStream bOutput = new ByteArrayOutputStream();
    bOutput.write("{\"Key1\": \"Value1\",\"Key2\": \"Value2\"}".getBytes());  

    try {
        connectAction connReq = new connectAction();

        connReq.setPost(true);
        connReq.addRequestHeader("Authorization", "1234");
        connReq.addRequestHeader("client_id","4321");
        connReq.addRequestHeader("Content-Type","application/json");
        connReq.setUrl("https://myapi.com/test");
        connReq.setHttpMethod("POST");
        connReq.buildRequestBody(bOutput);
        NetworkManager.getInstance().addToQueueAndWait(connReq);
        Map<String,Object> result = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(connReq.getResponseData()), "UTF-8"));
        return result; 
    }
    catch(Exception err) {
        System.err.println(err);
        return null;
    }  
现在,当我在public void buildRequestBody(OutputStream os)上放置断点时,我可以看到“os”具有该值

os=(java.io.ByteArrayOutputStream){“Key1”:“Value1”,“Key2”:“Value2”}

所以我知道,价值观一直在发挥作用

但是,当我启动网络监视器时,仅填充请求标头,标头为空

非常感谢您的帮助


谢谢

请尝试以下代码,希望对您有所帮助

// convert the object to a JSON document
HashMap hashtable = new HashMap ();
hashtable.put("Key1", "Value1");
hashtable.put("Key2", "Value2");
final String payload = Result.fromContent(hashtable).toString();

protected void buildRequestBody(OutputStream os) throws IOException {
    os.write(payload.getBytes("UTF-8"));
}

非常感谢!!!!!我一直在努力解决这个问题。我正要改变框架并寻找其他东西:-)。谢谢-谢谢!
// convert the object to a JSON document
HashMap hashtable = new HashMap ();
hashtable.put("Key1", "Value1");
hashtable.put("Key2", "Value2");
final String payload = Result.fromContent(hashtable).toString();

protected void buildRequestBody(OutputStream os) throws IOException {
    os.write(payload.getBytes("UTF-8"));
}