machinery
machinery

Reputation: 6290

Parsing JSON response from node js server in Java

I have a nodes js server with the following method:

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

So the server sends a json object back. In my Android client I would like to parse this response as follows:

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

Somehow the connection.getResponseMessage(); only returns "Ok" but not the actual json response. How can I parse the Json response from the server?

Upvotes: 0

Views: 168

Answers (1)

Govind
Govind

Reputation: 439

Can you please parse your response as below?

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

Upvotes: 1

Related Questions