【问题标题】:JSON Post request from Codename One来自 Codename One 的 JSON Post 请求
【发布时间】:2019-05-10 01:04:13
【问题描述】:

在过去的几天里,我一直在努力解决这个问题,我想我尝试了我在互联网上可以找到的每一个例子。我正在尝试登录我的 REST 并取回持续通信的授权代码。使用 Postman 时有效

----取自 Postman Generate Code Snippit----

POST /api/auth/signin HTTP/1.1
Host: localhost:5000
Content-Type: application/json
cache-control: no-cache
Postman-Token: baffa067-c2a0-4d5c-b321-348682227195
{
    "username": "Test",
    "password": "Testpw"
}------WebKitFormBoundary7MA4YWxkTrZu0gW--

然后它返回

{"accessToken":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiaWF0IjoxNTQ0Mjc4MTE5LCJleHAiOjE1NDQ4ODI5MTl9.qNQ7MIIsWpYQdeje6Ox88KR9AdNGCSiffjyk__nrdWvP7yoy4Ukk05ZQUISVptOIoBpahFT1OHf_MXVQnW5Izg","tokenType":"Bearer"}

我在 CodenameOne 项目中的代码是:

 public void onloginActionEvent(com.codename1.ui.events.ActionEvent ev) {
              JSONObject json = new JSONObject();
              try {
                  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 {
                        }

                        @Override
                        protected void postResponse() {                             
                        }
                    };
                    json.put("username", "Test");
                    json.put("password", "Testpw");

                  post.setUrl("http://localhost:5000/api/auth/signin");
                  post.setPost(true);
                  post.setContentType("application/json");
                  post.addArgument("body", json.toString());
                  String bodyToString = json.toString();
                  NetworkManager.getInstance().addToQueueAndWait(post);
                  Map<String,Object> result = new JSONParser().parseJSON(new InputStreamReader(new ByteArrayInputStream(post.getResponseData()), "UTF-8"));
              } catch (Exception ex) {
                  ex.printStackTrace();
              }
          }

当使用 CodenameOne Simulator 网络监视器有以下结果

URL: http://localhost:5000/api/auth/signin

Type: Post

Request:BodyPart: {"password":"Test","username":"Testpw"}


Request:Request Headers: User-Agent=[Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1]
Content-Type=[application/json]

Response:Body: blank

Response: Response Headers: Transfer-Encoding=[chunked]
X-Frame-Options=[DENY]
null=[HTTP/1.1 200]
Cache-Control=[no-cache, no-store, max-age=0, must-revalidate]
X-Content-Type-Options=[nosniff]
Expires=[0]
Pragma=[no-cache]
X-XSS-Protection=[1; mode=block]
Date=[Sat, 08 Dec 2018 14:14:54 GMT]
Content-Type=[application/json;charset=UTF-8]

【问题讨论】:

    标签: json codenameone


    【解决方案1】:

    您在不调用 super 的情况下覆盖 readResponse。这会覆盖读取数据的默认行为。您可以删除该方法或在该方法中编写读取 JSON 的逻辑。

    【讨论】:

      猜你喜欢
      • 2016-11-20
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      相关资源
      最近更新 更多