如何在codenameone中将JSON发布到REST Web服务
任何人都可以向我展示示例代码:如何在codenameone中将JSON发布到REST Web服务,json,rest,codenameone,Json,Rest,Codenameone,任何人都可以向我展示示例代码: 如何向REST Web服务发布JSON;及 如何从服务器读取JSON响应 使用代号1 以下是我尝试过的从服务器返回错误请求响应的方法: Button b1 = new Button("Add Staff"); b1.addActionListener(new ActionListener() { @Override public void actionPerformed(Action
JSON
;及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();
}
}
});
按钮b1=新按钮(“添加员工”);
b1.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件evt){
JSONObject json=新的JSONObject();
试一试{
ConnectionRequest post=新的ConnectionRequest(){
@凌驾
受保护的void postResponse(){
试一试{
put(“firstname”,fname.getText());
put(“middlename”,mname.getText());
put(“lastname”,lname.getText());
put(“dob”,dob.getText());
put(“gender”,gender.getSelectedItem().toString());
put(“monthly”,monthly.getSelectedItem().toString());
put(“phone”,phone.getText());
put(“adds”,adds.getText());
put(“用户名”,user.getText());
put(“password”,pass.getText());
put(“lat”,lat.getText());
put(“long”,lon.getText());
}捕获(JSONException ex){
例如printStackTrace();
}
}
@凌驾
受保护的void readResponse(InputStream输入)引发IOException{
}
};
post.setUrl(“http://localhost:8093/halimatbank/cbs/staff");
post.setPost(true);
post.setContentType(“应用程序/JSON”);
post.addArgument(“body”,json.toString());
boolean show=Dialog.show(“添加职员”、“是否确实要添加此职员”、“是”、“否”);
NetworkManager.getInstance().addToQueueAndWait(post);
Map result=new-JSONParser().parseJSON(新的InputStreamReader(新的ByteArrayInputStream(post.getResponseData()),“UTF-8”);
Map response=(Map)result.get(“response”);
显示(“员工已保存”和“+响应”、“确定”和“);
}捕获(例外情况除外){
例如printStackTrace();
}
}
});
postResponse()
在流程完成后调用。与post本身无关。您希望覆盖之前执行的buildRequestBody
。如果我理解正确,您希望整个主体是JSON,而不是名为“body”的参数,这就是您所做的…:
ConnectionRequest post = new ConnectionRequest(){
@Override
protected void buildRequestBody(OutputStream os) throws IOException {
os.write(json.toString().getBytes("UTF-8"));
}
@Override
protected void readResponse(InputStream input) throws IOException {
// parse response data
}
};
post.setUrl("http://localhost:8093/halimatbank/cbs/staff");
post.setPost(true);
post.setContentType("application/json");
因此,这既不是代码编写服务,也不是辅导服务,您的紧迫性与此无关。你的代码在哪里?有什么问题?请看。好的,谢谢,我已经编辑了我的问题。如果任何人不知道一个好的回答应该是什么样子,他们怎么能帮助呢?尝试删除内容类型并告诉我们答案是什么,然后改进了格式谢谢你这么多shai,它真的解决了我的问题,我现在可以向服务器发布和接收数据了。再次感谢