SoundCheese
SoundCheese

Reputation: 143

Android Volley onResponse get data

In a string post request for logging in. in onResponse all i am returned is the status code of 200. I would like to get the data returned as well. I am not sure where to go from this point forward? Any ideas on how to get the data returned, and not just the status code?

    public void requestWithSomeHttpHeaders() {

    try {
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String URL = "http://......";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("username", "yourusername");
        jsonBody.put("password", "yourpassword");
        final String mRequestBody = jsonBody.toString();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("VOLLEY", response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("VOLLEY", error.toString());
            }
        }) {
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }

            @Override
            public byte[] getBody() throws AuthFailureError {
                try {
                    return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
                } catch (UnsupportedEncodingException uee) {
                    VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
                    return null;
                }
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String responseString = "";
                if (response != null) {
                    responseString = String.valueOf(response.statusCode);
                    // can get more details such as response.headers
                    System.out.println(responseString);
                }
                return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
            }
        };

        requestQueue.add(stringRequest);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

Upvotes: 0

Views: 5258

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191728

Preferably, define a method.

private void doLogin() {
    // TODO: startActivity?
}

Then call that

 StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.i("VOLLEY", response);

            doLogin();
        }

If you want the data from the server, that is what String response is. Check your Logcat.

As mentioned in the comments, using StringRequest without implementing getBody, parseNetworkResponse, and getBodyContentType might work better.

Upvotes: 1

Cristian Cardoso
Cristian Cardoso

Reputation: 675

If you want to parse JSON add method like this

 public Profile getUserProfile(String response){
    Profile profile = null;
    try {
        profile = new Profile();
        JSONObject jsonObject = new JSONObject(response);
            profile.setId(jsonObject.getInt(context.getString(R.string.profile_id)));
        profile.setLastName(jsonObject.getString(context.getString(R.string.profile_lastName)));
        profile.setName(jsonObject.getString(context.getString(R.string.profile_name)));
        profile.setEmail(jsonObject.getString(context.getString(R.string.profile_email)));


    } catch (JSONException | NullPointerException e) {
        e.printStackTrace();
        return null;
    }

    return profile;
}

In your onResponse method

     @Override
        public void onResponse(String response) {
            //If you have son object
           Profile profile = getUserProfile(response)
        }

Upvotes: 1

Related Questions