1

I am working on Volley but i am quite new to this framework i have no idea how to post JSON data using volley, i have one sample JSON please tell me how to post this JSON data.

JSONObject jsonObject = new JSONObject();
jsonObject.put("email", "abcd@g.com");
jsonObject.put("password", "abcd123");
jsonObject.put("device", "jdghfdhgdhi");
jsonObject.put("latitude", 1.2456);
jsonObject.put("longitude", 1.3466);

Please kindly go through my JSON and let me know how to post this JSON using volley.

Anshuman Pattnaik
  • 143
  • 1
  • 1
  • 7
  • 1
    Possible duplicate of [Send POST request with JSON data using Volley](http://stackoverflow.com/questions/23220695/send-post-request-with-json-data-using-volley) – Sanoop Surendran Aug 08 '16 at 06:16
  • http://stackoverflow.com/questions/23220695/send-post-request-with-json-data-using-volley Look on that answer – Arjun saini Aug 08 '16 at 06:17
  • http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/ go through about volley. – PRIYA PARASHAR Aug 08 '16 at 06:22
  • hey thanks but in my JSON i have two double value can you tell me the code because i am completely new to Volley ??? – Anshuman Pattnaik Aug 08 '16 at 06:22
  • [http://stackoverflow.com/questions/38471006/android-volley-how-to-receive-and-send-a-json/38472251#38472251](http://stackoverflow.com/questions/38471006/android-volley-how-to-receive-and-send-a-json/38472251#38472251) check here once – Riten Aug 08 '16 at 06:40

2 Answers2

0

try this:

    StringRequest stringRequest = new StringRequest(Request.Method.POST, your url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(NoolReaderDashboard.this, error.toString(), Toast.LENGTH_LONG).show();
                        }
                    }) {
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<String, String>();
           params .put("email", "abcd@g.com");
         params .put("password", "abcd123");
        params .put("device", "jdghfdhgdhi");
        params .put("latitude", 1.2456);
         params .put("longitude", 1.3466);
                    return params;
                }
            };
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
0

private void Postdata() {

    String url = "http://www.example.com";                              //url where you want to post data
    StringRequest postRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        Log.i("response is","==>"+response);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Log.d("Response-------", "------>" + response);
                }
            },
            new Response.ErrorListener() {
                public void onErrorResponse(VolleyError error) {
                    Log.e("Responce error==","===>"+error);
                    error.printStackTrace();
                }
            }
    ) {

        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            // the POST parameters:
            params.put("email", "abcd@g.com");
            params.put("password", "abcd123");
            params.put("latitude", 1.2456);
            params.put("longitude", 1.3466);
            Log.d("Value is ----------", ">" + params);
            return params;
        }
    };
    MyApplication .getInstance().addToRequestQueue(postRequest);
}
Amit Basliyal
  • 840
  • 1
  • 10
  • 28