Requests Java (Android, really)
DISCLAIMER: INSPIRED BY BUT NOT ASSOCIATED WITH https://github.com/psf/requests
The build is hosted on jcenter so you could just add the line:
implementation 'pk.codebase.requests:requests:0.6'
The API is written for simplicity, below examples provide a peak into it.
HttpRequestrequest = newHttpRequest();
request.setOnResponseListener(newHttpRequest.OnResponseListener() {
@OverridepublicvoidonResponse(HttpResponseresponse) {
if (response.code == HttpResponse.HTTP_OK) {
System.out.println(response.toJSONObject());
}
}
});
request.setOnErrorListener(newHttpRequest.OnErrorListener() {
@OverridepublicvoidonError(HttpErrorerror) {
// There was an error, deal with it
}
});
request.get("https://httpbin.org/get");HttpRequestrequest = newHttpRequest();
request.setOnResponseListener(newHttpRequest.OnResponseListener() {
@OverridepublicvoidonResponse(HttpResponseresponse) {
if (response.code == HttpResponse.HTTP_OK) {
System.out.println(response.toJSONObject());
}
}
});
request.setOnErrorListener(newHttpRequest.OnErrorListener() {
@OverridepublicvoidonError(HttpErrorerror) {
// There was an error, deal with it
}
});
JSONObjectjson;
try {
json = newJSONObject();
json.put("foo", "bar");
json.put("jane", "doe");
} catch (JSONExceptionignore) {
return;
}
request.post("https://httpbin.org/post", json);These methods are also supported and expected the same set of parameters as POST. The convenience methods are request.put(); request.patch(); request.delete();
Headers can be sent using the HttpHeaders class.
HttpHeadersheaders = newHttpHeaders("Authorization", "Token sasdasdai2sadas")
request.get("https://httpbin.org/get", headers);FormDatadata = newFormData();
data.put("foo", "bar");
data.put("file2", newFile("/sdcard/image.png"));
request.post("https://httpbin.org/post", data);