在过去的几天里,我一直在为此苦苦挣扎,我想我尝试了可以在互联网上找到的每个示例。我正在尝试登录REST并获取授权代码以进行持续的通信。使用邮递员
时可以使用----从邮递员那里生成代码片段----
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模拟器网络监视器时,将得到以下结果
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]
答案 0 :(得分:1)
您覆盖readResponse
而不调用super
。这将覆盖读取数据的默认行为。您可以删除该方法或编写用于读取该方法中JSON的逻辑。