Last active
June 8, 2022 20:06
-
-
Save dmytrodanylyk/e41a4940f1c602868401 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Android Volley |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AuthRequestSample { | |
public void send(RequestQueue requestQueue) { | |
Uri.Builder uri = new Uri.Builder(); | |
uri.scheme("http"); | |
uri.authority("httpbin.org"); | |
uri.path("basic-auth/user/passwd"); | |
String url = uri.build().toString(); | |
StringRequest request = new StringRequest( | |
Request.Method.GET, | |
url, | |
listener, | |
errorListener) { | |
@Override | |
public Map<String, String> getHeaders() throws AuthFailureError { | |
return createBasicAuthHeader("user", "passwd"); | |
} | |
}; | |
requestQueue.add(request); | |
} | |
Map<String, String> createBasicAuthHeader(String username, String password) { | |
Map<String, String> headerMap = new HashMap<String, String>(); | |
String credentials = username + ":" + password; | |
String base64EncodedCredentials = | |
Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); | |
headerMap.put("Authorization", "Basic " + base64EncodedCredentials); | |
return headerMap; | |
} | |
Response.Listener<String> listener = new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
L.d("Success Response: " + response.toString()); | |
} | |
}; | |
Response.ErrorListener errorListener = new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
if (error.networkResponse != null) { | |
L.d("Error Response code: " + error.networkResponse.statusCode); | |
} | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GetRequestSample { | |
public void send(RequestQueue requestQueue) { | |
Uri.Builder uri = new Uri.Builder(); | |
uri.scheme("http"); | |
uri.authority("httpbin.org"); | |
uri.path("get"); | |
uri.appendQueryParameter("name", "Jon Doe"); | |
uri.appendQueryParameter("age", "21"); | |
String url = uri.build().toString(); | |
StringRequest request = new StringRequest( | |
Request.Method.GET, | |
url, | |
listener, | |
errorListener); | |
requestQueue.add(request); | |
} | |
Response.Listener<String> listener = new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
L.d("Success Response: " + response.toString()); | |
} | |
}; | |
Response.ErrorListener errorListener = new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
if (error.networkResponse != null) { | |
L.d("Error Response code: " + error.networkResponse.statusCode); | |
} | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JSONRequestSample { | |
public void send(RequestQueue requestQueue) { | |
Uri.Builder uri = new Uri.Builder(); | |
uri.scheme("http"); | |
uri.authority("httpbin.org"); | |
uri.path("get"); | |
uri.appendQueryParameter("name", "Jon Doe"); | |
uri.appendQueryParameter("age", "21"); | |
String url = uri.build().toString(); | |
// JsonArrayRequest if root element is JSON array | |
JsonObjectRequest request = new JsonObjectRequest( | |
Request.Method.GET, | |
url, | |
null, | |
listener, | |
errorListener); | |
requestQueue.add(request); | |
} | |
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() { | |
@Override | |
public void onResponse(JSONObject response) { | |
L.d("Success Response: " + response.toString()); | |
} | |
}; | |
Response.ErrorListener errorListener = new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
if (error.networkResponse != null) { | |
L.d("Error Response code: " + error.networkResponse.statusCode); | |
} | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PostRequestSample { | |
public void send(RequestQueue requestQueue) { | |
Uri.Builder uri = new Uri.Builder(); | |
uri.scheme("http"); | |
uri.authority("httpbin.org"); | |
uri.path("post"); | |
String url = uri.build().toString(); | |
StringRequest request = new StringRequest( | |
Request.Method.POST, | |
url, | |
listener, | |
errorListener) { | |
@Override | |
protected Map<String, String> getParams() throws AuthFailureError { | |
Map<String, String> map = new HashMap<String, String>(); | |
map.put("name", "Jon Doe"); | |
map.put("age", "21"); | |
return map; | |
} | |
}; | |
requestQueue.add(request); | |
} | |
Response.Listener<String> listener = new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
L.d("Success Response: " + response.toString()); | |
} | |
}; | |
Response.ErrorListener errorListener = new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
if (error.networkResponse != null) { | |
L.d("Error Response code: " + error.networkResponse.statusCode); | |
} | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StatusRequestSample { | |
public void send(RequestQueue requestQueue) { | |
Uri.Builder uri = new Uri.Builder(); | |
uri.scheme("http"); | |
uri.authority("httpbin.org"); | |
uri.path("status/" + HttpStatus.SC_METHOD_NOT_ALLOWED); | |
String url = uri.build().toString(); | |
StringRequest request = new StringRequest( | |
Request.Method.GET, | |
url, | |
listener, | |
errorListener); | |
requestQueue.add(request); | |
} | |
Response.Listener<String> listener = new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
L.d( "Success Response: " + response.toString()); | |
} | |
}; | |
Response.ErrorListener errorListener = new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
L.d("Error Response code: " + error.getMessage()); | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TimeoutRequestSample { | |
/** | |
* Delays responding for 5 seconds. Default request timeout time is 2.5 seconds. Request should | |
* fail. | |
*/ | |
public void send(RequestQueue requestQueue) { | |
Uri.Builder uri = new Uri.Builder(); | |
uri.scheme("http"); | |
uri.authority("httpbin.org"); | |
uri.path("delay/5"); | |
String url = uri.build().toString(); | |
StringRequest request = new StringRequest( | |
Request.Method.GET, | |
url, | |
listener, | |
errorListener); | |
request.setRetryPolicy( | |
new DefaultRetryPolicy( | |
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, | |
DefaultRetryPolicy.DEFAULT_MAX_RETRIES, | |
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); | |
requestQueue.add(request); | |
} | |
Response.Listener<String> listener = new Response.Listener<String>() { | |
@Override | |
public void onResponse(String response) { | |
L.d("Success Response: " + response.toString()); | |
} | |
}; | |
Response.ErrorListener errorListener = new Response.ErrorListener() { | |
@Override | |
public void onErrorResponse(VolleyError error) { | |
if (error.networkResponse != null) { | |
L.d("Error Response code: " + error.networkResponse.statusCode); | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment