繁体   English   中英

发送代码名称为 1 的 post 请求时,Json 字符串为空

[英]when sending post request with codeName one, Json String is empty

我正在尝试使用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);

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM