【问题标题】:when sending post request with codeName one, Json String is empty发送带有codeName 1的post请求时,Json String为空
【发布时间】:2018-03-12 07:00:11
【问题描述】:

我正在尝试在CodeName 中发送一个带有 JSON BODY 的 POST 请求。

它以一个空的 Json 字符串到达​​服务器。

这是建立连接并发送消息的代码:

JSONObject json = new JSONObject();

class MyConnection extends ConnectionRequest {
    public Map<String, Object> results;

    @Override
    protected void readResponse(InputStream input) throws IOException {
        JSONParser jp = new JSONParser();
        results = jp.parseJSON(new InputStreamReader(input, "UTF-8"));
    }

    @Override
    protected void handleErrorResponseCode(int code, String message) {
        showError("The server returned the error code: " + code);
    }

    @Override
    protected void handleException(Exception err) {
        showError("There was a connection error: " + err);
    }

    @Override
    protected void postResponse() {
        try {
            json.put("AAA", "AAA");
            json.put("BBB", "BBB");
            json.put("CCC", "CCC");
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    protected void buildRequestBody(OutputStream os) throws IOException {
        os.write(json.toString().getBytes("UTF-8"));
    }
}

---

MyConnection connec = new MyConnection ();

connec.setUrl("http://testServoce/addUser");
connec.setPost(true);
connec.setContentType("application/json");

InfiniteProgress prog = new InfiniteProgress();
Dialog dlg = prog.showInifiniteBlocking();
connec.setDisposeOnCompletion(dlg);

NetworkManager.getInstance().addToQueueAndWait(connec);

【问题讨论】:

    标签: java json codenameone


    【解决方案1】:

    我不确定您的意图是什么,但您似乎误解了 postResponse 方法的目标。它与POST web 方法无关,只是在 EDT 上的响应完成后调用。所以改变那里的 JSON 值是无关紧要的。

    此外,您似乎出于某种原因使用了两个单独的 JSON 解析器。内置的一个和来自 cn1libs 之一的org.json

    【讨论】:

    • 非常感谢 shai,从 postResponse() 中删除设置 Json 值后,它确实解决了我的问题。再次感谢
    猜你喜欢
    • 2021-03-12
    • 1970-01-01
    • 2015-05-19
    • 2019-05-10
    • 2020-02-29
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    相关资源
    最近更新 更多