Search code examples
androidgetandroid-volley

How to send parameters to backend, and get response message, using GET method in volley, android?


I want to send three parameters "guestEmail", "latitude" and "longitude" to backend and get a message of success from backend if it is successful.

I have tried doing this:

public void myGetFunc()
{

final String url = "....";

// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
    new Response.Listener<JSONObject>() 
    {
        @Override
        public void onResponse(JSONObject response) {   
                        // display response     
            Log.d("Response", response.toString());
Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_SHORT).show();
        }
    }, 
    new Response.ErrorListener() 
    {
         @Override
         public void onErrorResponse(VolleyError error) {            
            Log.d("Error.Response", response);
       }
    }
)

{

    @Override
    protected Map<String, String> getParams() 
    {  
            Map<String, String>  params = new HashMap<String, String> ();  
            params.put("guestEmail", "[email protected]");  
            params.put("latitude", "12");
            params.put("longitude", "12");

            return params;  
    }
};

// add it to the RequestQueue   
queue.add(getRequest);
}

This method is invoked when the 'SOS' button is clicked. But right now, nothing happens on clicking the 'SOS' button.

Please help!


Solution

  • If you are going to use GET you query parameters and build the string yourself

    private static final String URL = "http://www.test.com?value1={val1}&value2={val2}";
    
    String requestString = URL;
    requestString.replace("{val1}", "1");
    requestString.replace("{val2}", "Bob");
    
    StringRequest strreq = new StringRequest(Request.Method.GET,
         requestString,
          new Response.Listener<String>() {
             @Override
             public void onResponse(String Response) {
                      // get response
             }
          }, new Response.ErrorListener() {
             @Override
             public void onErrorResponse(VolleyError e) {
                e.printStackTrace();
             }
    });
    Volley.getInstance(this).addToRequestQueue(strreq);
    

    If you are going to use POST us a body

    public class LoginRequest extends Request<String> {
    
        // ... other methods go here
    
        private Map<String, String> mParams;
    
        public LoginRequest(String param1, String param2, Listener<String> listener, ErrorListener errorListener) {
            super(Method.POST, "http://test.url", errorListener);
            mListener = listener;
            mParams = new HashMap<String, String>();
            mParams.put("paramOne", param1);
            mParams.put("paramTwo", param2);
    
        }
    
        @Override
        public Map<String, String> getParams() {
            return mParams;
        }
    }