从Java中的节点js服务器解析JSON响应

问题描述 投票:0回答:1

我有一个采用以下方法的节点js服务器:

app.get('/GetValues*', function (request, response) {
    // Request needs to be a GET
    if (request.method == 'GET') {

        var username = request.query.account;
        var time_now = Date.now();

        var db = database('./database.db'); 
        var row = db.prepare('SELECT SCORE_OFFSET score_offset, STARTED_STUDY_SERVER_MILLIS timestamp, DAYS_TOTAL days_total, DURATION_ENABLED_OFFSET duration_enabled_offset, DURATION_DISABLED_OFFSET duration_disabled_offset FROM ACCOUNTS WHERE NAME = ?').get(username);

        var score_offset = row.score_offset;
        var days_total = row.days_total;
        if (row.timestamp && (row.timestamp > 0)) {
            var days_count = (time_now - row.timestamp) / (24 * 60 * 60 * 1000);
        } else {
            var days_count = 0;
        }

        var statement = db.prepare("UPDATE ACCOUNTS SET DAYS_COUNT = ? WHERE ID = ?");
        statement.run(days_count,getAccountID(db, request.query.account));

        var duration_enabled_offset = row.duration_enabled_offset;
        var duration_disabled_offset = row.duration_disabled_offset;

        var stmt = db.prepare("UPDATE ACCOUNTS SET DURATION_ENABLED_OFFSET = ?, DURATION_DISABLED_OFFSET = ?, SCORE_OFFSET = ? WHERE ID = ?");
        stmt.run([0, 0, 0, getAccountID(db, request.query.account)]);

        response.json({
            score_offset: score_offset,
            days_total: days_total,
            days_count: days_count,
            duration_enabled_offset: duration_enabled_offset,
            duration_disabled_offset: duration_disabled_offset
        });
    }
});

因此服务器将json对象发送回。在我的Android客户端中,我想解析此响应,如下所示:

        HttpsURLConnection connection = null;
        // Get URL to server for uploading
        String query = String.format("account=%s",  mAccountName);
        URL url = new URL(SettingValues.SERVER_ADDRESS + "/" + HTTPS_GET_VALUES + "?" + query);
        connection = (HttpsURLConnection) url.openConnection();

        connection.setSSLSocketFactory(mSSLContext.getSocketFactory());
        connection.setDoInput(true);
        connection.setDoOutput(false);
        connection.setUseCaches(false);
        connection.setConnectTimeout(HTTPS_TIMEOUT_VALUE);
        connection.setReadTimeout(HTTPS_READ_TIMEOUT_VALUE);

        connection.setRequestMethod("GET");
        connection.setRequestProperty("Connection", "Keep-Alive");
        String serverResponseMessage = connection.getResponseMessage();
        JSONObject obj = new JSONObject(serverResponseMessage);

以某种方式,connection.getResponseMessage();仅返回“确定”,而不返回实际的json响应。如何解析服务器的Json响应?

java android node.js
1个回答
1
投票

您能否按以下方式分析您的回复?

try(BufferedReader br = new BufferedReader(
  new InputStreamReader(connection.getInputStream(), "utf-8"))) {
    StringBuilder response = new StringBuilder();
    String responseLine = null;
    while ((responseLine = br.readLine()) != null) {
        response.append(responseLine.trim());
    }
    System.out.println(response.toString());
}
© www.soinside.com 2019 - 2024. All rights reserved.