如何使用CodenameOne将JSON发布到REST

如何使用CodenameOne将JSON发布到REST

本文介绍了如何使用CodenameOne将JSON发布到REST Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以通过示例代码向我展示

Can anyone can show me, with sample codes:

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

  1. How to post JSON to a REST webservice; and

如何从服务器读取JSON响应;

How to read the JSON response from the server;

使用代号One?

这是我尝试从服务器返回错误请求响应的内容:

Here is what i have tried which is returning bad request response from the server:

        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();
                }
            }
        });

该过程完成后,将调用

推荐答案

postResponse().与帖子本身无关.您要覆盖之前执行的buildRequestBody.如果我理解正确,那么您希望整个主体都是JSON,而不是您所做的名为"body"的参数...:

postResponse() is invoked after the process completes. Not related to post itself. You want to override buildRequestBody which executes before. If I understand correctly you want the entire body to be the JSON and not an argument named "body" which is what you did...:

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");

这篇关于如何使用CodenameOne将JSON发布到REST Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:45