【问题标题】:How to post JSON to a REST webservice in codenameone如何将 JSON 发布到 codenameone 中的 REST Web 服务
【发布时间】:2016-12-28 01:59:04
【问题描述】:

任何人都可以告诉我,带有示例代码:

  1. 如何将JSON 发布到 REST Web 服务;和

  2. 如何从服务器读取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


【解决方案1】:

postResponse() 在进程完成后调用。与帖子本身无关。您想覆盖之前执行的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,它确实解决了我的问题,我现在可以将数据发布和接收到服务器。再次感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多