Points to remember if you're using Volley

Posted on May 27, 2018

Tagged with
Andriod

If you're using volley for the api calls in your android application these are the importants that you need to remember

 

Step 1 : 

Either Clone volley 

git clone https://github.com/google/volley

Step 2 : 

Have permission in Android Manifest

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

Step 3 :

Api calls - Get and Post Method

public void doLogin(View v)
{
    //Email
    EditText emailTxt = (EditText)findViewById(R.id.email);
    final String email = emailTxt.getText().toString();
    //Password
    EditText passwordTxt = (EditText)findViewById(R.id.password);
    final String password = passwordTxt.getText().toString();
    RequestQueue queue = Volley.newRequestQueue(this);

    StringRequest postRequest = new StringRequest(Request.Method.POST, doLogin,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject mainObject = new JSONObject(response);
                        Integer successVal = mainObject.getInt("success");
                        if(successVal==0)
                        {
                            JSONObject mainObjects = new JSONObject(response);
                            String successMsg = mainObjects.getString("message");
                            Toast.makeText(getApplicationContext(), successMsg, Toast.LENGTH_LONG).show();
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(), "Logged in Successfully", Toast.LENGTH_LONG).show();
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                        }
                    } catch (JSONException e) {
                        Log.e("MYAPP", "unexpected JSON exception", e);
                    }
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("Error.Response", error.toString());
                }
            }
    ) {
        @Override
        protected Map<String, String> getParams()
        {
            Map<String, String>  params = new HashMap<String, String>();
            params.put("email", email);
            params.put("password", password);
            return params;
        }
    };
    queue.add(postRequest);
    //End
}

public void getToken(){
    Toast.makeText(this, "Inside token", Toast.LENGTH_LONG).show();
    RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest postRequest = new StringRequest(Request.Method.GET, getToken,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject mainObject = new JSONObject(response);
                        Integer successVal = mainObject.getInt("success");
                        if(successVal==1)
                        {
                            JSONObject mainObjects = new JSONObject(response);
                            String Token = mainObjects.getString("_token");
                            Toast.makeText(getApplicationContext(), "Token is "+Token, Toast.LENGTH_LONG).show();
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        Log.e("MYAPP", "unexpected JSON exception", e);
                    }
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("Error.Response", error.toString());
                }
            }
    ) {
    };
    queue.add(postRequest);
}

 

These 3 simple steps will make volley out there in your Project !!