[英]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.