How to Use the Android Volley API

How do you use the Android Volley API?

Edit: finally here it is an official training about "Volley library"

I found some examples about Volley library

  • 6 examples by Ognyan Bankov :

    • Simple request
    • JSON request
    • Gson request
    • Image loading
    • with newer external HttpClient (4.2.3)
    • With Self-Signed SSL Certificate.
  • one good simple example by Paresh Mayani

  • other example by HARDIK TRIVEDI

  • (NEW) Android working with Volley Library by Ravi Tamada

Hope this helps you

How to read values from an API using volley?

Assuming that sections: {...} is the response

You're trying to read sections as a JSONArray but is an object, you have to read it like this:

jsonObject = new JSONObject(String.valueOf(response));
Iterator<String> sectionKeys = jsonObject.keys();
while(sectionKeys.hasNext()){
String sectionKey = iter.next();
if (sectionKey.equals("enabled")) {
Boolean enabled = jsonObject.getBoolean("enabled");
...
} else {
JSONObject sectionObject = jsonObject.getJSONObject(sectionKey);
Boolean active = sectionObject.getBoolean("active")
...
}
}

How to use POST request in android Volley library with params and header?

**Try this one **

    private void sendWorkPostRequest() {

try {
String URL = "";
JSONObject jsonBody = new JSONObject();

jsonBody.put("email", "abc@abc.com");
jsonBody.put("password", "");
jsonBody.put("user_type", "");
jsonBody.put("company_id", "");
jsonBody.put("status", "");

JsonObjectRequest jsonOblect = new JsonObjectRequest(Request.Method.POST, URL, jsonBody, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {

Toast.makeText(getApplicationContext(), "Response: " + response.toString(), Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

onBackPressed();

}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Basic " + "c2FnYXJAa2FydHBheS5jb206cnMwM2UxQUp5RnQzNkQ5NDBxbjNmUDgzNVE3STAyNzI=");//put your token here
return headers;
}
};
VolleyApplication.getInstance().addToRequestQueue(jsonOblect);

} catch (JSONException e) {
e.printStackTrace();
}
// Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();

}
}

How to do login using volley?

Look at my following source code

//Creating a string request
StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.LOGIN_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {

Log.d("Response",""+response);
//If we are getting success from server
if (response.equals("success")) {
//Creating a shared preference

SharedPreferences sp = LoginActivity.this.getSharedPreferences(Constant.SHARED_PREF_NAME, Context.MODE_PRIVATE);

//Creating editor to store values to shared preferences
SharedPreferences.Editor editor = sp.edit();
//Adding values to editor
editor.putString(Constant.ROLL_SHARED_PREF, roll);

//Saving values to editor
editor.apply();

//Starting Home activity
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();

}

else if(response.equals("failure")) {
//If the server response is not success
//Displaying an error message on toast
Toast.makeText(LoginActivity.this, "Roll or Password is not valid", Toast.LENGTH_LONG).show();
}

else {
//If the server response is not success
//Displaying an error message on toast
Toast.makeText(LoginActivity.this, "Invalid user cell or password", Toast.LENGTH_LONG).show();
}
}
},

new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//You can handle error here if you want

Toast.makeText(LoginActivity.this, "There is an error !!!", Toast.LENGTH_LONG).show();
loading.dismiss();
}
}) {

@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
//Adding parameters to request
params.put(Constant.KEY_ROLL, roll);
params.put(Constant.KEY_PASSWORD, password);

//returning parameter
return params;
}
};

//Adding the string request to the queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}

I took this source code from my git repository
Maybe if you visit the following link than you will understand it more properly.

Volley login activity



Related Topics



Leave a reply



Submit