Android Volley – HTTP Library for Networking
Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub.
Documentation Link:- https://developer.android.com/training/volley/
Requires internet permission for accessing content.
<!-- requires internet permission -->
<uses-permission android:name="android.permission.INTERNET" />
dependencies {
...
implementation 'com.android.volley:volley:1.1.1'
}
Java code for simple get request
final TextView mTextView = (TextView) findViewById(R.id.text);
// ...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
Textview is for reference only, can be replaced with your custom url.
Java code for post request
//TODO- change url
String url = "YOUR-URL";
final StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//TODO-handle response data
/**HANDLE YOUR RESPONSE DATA HERE**/
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
/**HANDLE NETWORK ERROR**/
error.printStackTrace();
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
//TODO-add your custom parameters
params.put("parameter-key","parameter-value");
return params;
}
};
Volley.newRequestQueue(listener).add(postRequest);
Library Details:-
- Link:- https://developer.android.com/training/volley/
- Github:- https://github.com/google/volley
- Current Version:- 1.1.1
- License:- Apache 2.0
Credits:-
Codes from android developers