【发布时间】: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