June 20, 2018

API call using Volley StringRequest

          On this blog I am going to explain the API call using volley library, Here the API call is using a StringRequest.  For the API integration we are using Volley Library , So first we need to add the volley library dependency on the application's build.gradle file like,

    dependencies {
         compile 'com.android.volley:volley:1.1.0'
    }


StringRequest 

    String serviceUrl = "http://shidhin.net/api/ApiTest";
        
    StringRequest strReq = new StringRequest(Request.Method.POST, serviceUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String responseString) {
                try {

                    // Successfull request
                    
                } catch (Exception e) {
                    Toast.makeText(LoginActivity.this, "Api Error", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(LoginActivity.this, "Api Error", Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            protected String getParamsEncoding() {
                return "utf-8";
            }
        };


This is the simple request, If we want to pass the request parameters, Header or request encoding type etc, we can do it like,
Request Parameters

   @Override       
   public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String>  params = new HashMap<String, String>();
        params.put("Accept","application/json");
params.put("Content-Type","application/json");
return params;
    }


Request Header

       @Override         
   public Map<String, String> getHeaders() throws AuthFailureError {
       Map<String, String>  params = new HashMap<String, String>();         params.put("Accept","application/json");     
       params.put("Content-Type","application/json");     
       return params; 
   } 




Final Reqest

This is an example StringRequest with Request Parameters and Request Header ( Request Parameters and Request Header are optional on a request).


StringRequest strReq = new StringRequest(Request.Method.POST, serviceUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String responseString) {
                try {
                    
                    // Successfull request

                } catch (Exception e) {
                    Toast.makeText(LoginActivity.this, "Api Error", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(LoginActivity.this, "Api Error", Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("username", userName);
                params.put("Password", password);
                return params;
            }
            
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("username", userName);
                params.put("Password", password);
                return params;
            }
            @Override
            protected String getParamsEncoding() {
                return "utf-8";
            }
        }
    });
com.android.volley.RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(strReq);




---------------
Thanks For Reading, Wish you a Happy Coding....