Java 发送代码名为1的post请求时,Json字符串为空
我正在尝试发送一个POST请求,其中包含Java 发送代码名为1的post请求时,Json字符串为空,java,json,codenameone,Java,Json,Codenameone,我正在尝试发送一个POST请求,其中包含CodeNameone中的JSON正文 它以空Json字符串到达服务器 以下是建立连接并发送消息的代码: JSONObject json = new JSONObject(); class MyConnection extends ConnectionRequest { public Map<String, Object> results; @Override protected void readResponse(
CodeName
one中的JSON正文
它以空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"));
}
}
我不确定您的意图是什么,但看起来您误解了
postResponse
方法的目标。它与POST
web方法无关,仅在EDT上完成响应后调用。因此,在那里更改JSON值是不相关的
此外,出于某种原因,您似乎正在使用两个单独的JSON解析器。内置的和CN1lib中的
org.json
一个。非常感谢shai,在从postResponse()中删除设置json值后,它真正解决了我的问题。再次感谢
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);