Ajay Reddy
Ajay Reddy

Reputation: 147

Sending json body in a post message from android phone

Hey I am looking to send this { "Longitude" : "123.123", "Lattitude" : "55.55" } to my site with no other requirements from an android phone with volley. the methods dont seem to be working. can any one help me

Thank you in advance

Upvotes: 0

Views: 79

Answers (1)

jagapathi
jagapathi

Reputation: 1645

As per your question, you need to send two params to your site

this is sample code of volley to send POST data with params

public void PostToServer(String URL, final HashMap Params) {
    try {

        RequestQueue queue = Volley.newRequestQueue(networkContext);
        StringRequest strreq = new StringRequest(Request.Method.POST,URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String Response) {
                        try {
                           //your server response
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError e) {

                e.printStackTrace();
            }
        }){@Override
        public Map<String, String> getParams(){
            return Params;
        }
        };

        queue.add(strreq);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

And call this function like this

HashMap<String, String> params = new HashMap<>();
                params.put("Longitude", "12.123");
                params.put("Lattitude", "12.145");
               PostToServer("http://example.com/send.php", params);

Upvotes: 1

Related Questions