【发布时间】:2016-12-28 01:59:04
【问题描述】:
任何人都可以告诉我,带有示例代码:
如何将
JSON发布到 REST Web 服务;和如何从服务器读取
JSON响应;
使用代号一?
这是我尝试过的,它从服务器返回错误的请求响应:
Button b1 = new Button("Add Staff");
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
JSONObject json = new JSONObject();
try {
ConnectionRequest post = new ConnectionRequest(){
@Override
protected void postResponse() {
try {
json.put("firstname", fname.getText());
json.put("middlename", mname.getText());
json.put("lastname", lname.getText());
json.put("dob", dob.getText());
json.put("gender", gender.getSelectedItem().toString());
json.put("marital", marital.getSelectedItem().toString());
json.put("phone", phone.getText());
json.put("adds", adds.getText());
json.put("username", user.getText());
json.put("password", pass.getText());
json.put("lat", lat.getText());
json.put("long", lon.getText());
} catch (JSONException ex) {
ex.printStackTrace();
}
}
@Override
protected void readResponse(InputStream input) throws IOException {
}
};
post.setUrl("http://localhost:8093/halimatbank/cbs/staff");
post.setPost(true);
post.setContentType("APPLICATION/JSON");
post.addArgument("body", json.toString());
boolean show = Dialog.show("Add Staff", "Are you Sure you want to add this Staff", "Yes", "NO");
NetworkManager.getInstance().addToQueueAndWait(post);
Map<String,Object> result = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(post.getResponseData()), "UTF-8"));
Map<String, Object> response = (Map<String, Object>)result.get("response");
Dialog.show("Staff Saved", ""+response, "OK","");
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
【问题讨论】:
-
SO 既不是代码编写也不是教程服务,您的紧迫性在这里无关紧要。你的代码在哪里,它有什么问题?见How to Ask。
-
好的,谢谢,我已经编辑了我的问题
-
给minimal reproducible example - 如果他们不知道一个好的回复应该是什么样子,他们怎么能提供帮助?
-
尝试删除内容类型,然后告诉我们响应是什么
-
改进格式
标签: json rest codenameone