0

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?

2
  • Can you try to extract the response as below? try(BufferedReader br = new BufferedReader( new InputStreamReader(conection.getInputStream(), "utf-8"))) { StringBuilder response = new StringBuilder(); String responseLine = null; while ((responseLine = br.readLine()) != null) { response.append(responseLine.trim()); } System.out.println(response.toString()); } Commented Jun 6, 2020 at 15:28
  • Glad that it worked for you, could you please vote and mark it as answer? Commented Jun 6, 2020 at 16:31

1 Answer 1

1

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());
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.