Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

I am using Android Volley library for network request, unable to post parameters to server using JsonObjectRequest

Hi have anyone used Android Volley library to post json parameters to server?

I followed example on this link to send name and password. http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

code: '''android { String tag_json_obj = "json_obj_req";

String url = "http://api.androidhive.info/volley/person_object.json";

ProgressDialog pDialog = new ProgressDialog(this); pDialog.setMessage("Loading..."); pDialog.show();

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
            url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
                    pDialog.hide();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    pDialog.hide();
                }
            }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", "Androidhive");
            params.put("email", "abc@androidhive.info");
            params.put("password", "password123");

            return params;
        }

    };

// Adding request to request queue

AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj); } '''

I just couldn't figure out why on server side parameters are always null. I used another network library AsyncTask, everything worked, so it appeared something wrong with this code for me. However, I want to use Volley rather than AsyncTask.

Anyone has idea?

1 Answer