1

I am trying to get response from my node.js server with MySQL database.

When I connect to server with my browser I get result like this:

[{"person_id":0,"age":18},{"person_id":1,"age":17},{"person_id":2,"age":30}]

What I want to do is to get the same result with my Android app after pressing the button.

I wanted to use LoopJ AndroidAsyncHttp:

AsyncHttpClient client = new AsyncHttpClient();
client.get("http://localhost:3000", new JsonHttpResponseHandler() {
       @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
           super.onSuccess(statusCode, headers, response);
        }
});

I know that I connected to server properly because I got log in server's console. What is the easiest way to retrieve that data?

Stephane Landelle
  • 6,990
  • 2
  • 23
  • 29

1 Answers1

1

Create a model Person contain "person_id" and "age"

AsyncHttpClient client = new AsyncHttpClient();
        client.get("http://localhost:3000",new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                try {
                    String response = responseBody == null ? null : new String(
                            responseBody, getCharset());
                    Log.d("Response: ", response);
                    Gson gson = new Gson();
                    Person[] arr = gson.fromJson(response, Person[].class);

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
Fire Stork
  • 71
  • 1
  • 5