https://github.com/codebasepk/requestsj
Inspired by Python's Requests
https://github.com/codebasepk/requestsj
android http requests
Last synced: 2 days ago
JSON representation
Inspired by Python's Requests
- Host: GitHub
- URL: https://github.com/codebasepk/requestsj
- Owner: codebasepk
- Created: 2017-08-02T17:30:50.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2022-01-10T09:24:11.000Z (over 4 years ago)
- Last Synced: 2023-06-30T00:43:58.471Z (about 3 years ago)
- Topics: android, http, requests
- Language: Java
- Homepage:
- Size: 190 KB
- Stars: 2
- Watchers: 7
- Forks: 6
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# RequestsJ
Requests Java (Android, really)
DISCLAIMER: INSPIRED BY BUT NOT ASSOCIATED WITH https://github.com/psf/requests
# Getting started
The build is hosted on jcenter so you could just add the line:
```
implementation 'pk.codebase.requests:requests:0.6'
```
# Usage
The API is written for simplicity, below examples provide a peak into it.
### GET
```java
HttpRequest request = new HttpRequest();
request.setOnResponseListener(new HttpRequest.OnResponseListener() {
@Override
public void onResponse(HttpResponse response) {
if (response.code == HttpResponse.HTTP_OK) {
System.out.println(response.toJSONObject());
}
}
});
request.setOnErrorListener(new HttpRequest.OnErrorListener() {
@Override
public void onError(HttpError error) {
// There was an error, deal with it
}
});
request.get("https://httpbin.org/get");
```
### POST
```java
HttpRequest request = new HttpRequest();
request.setOnResponseListener(new HttpRequest.OnResponseListener() {
@Override
public void onResponse(HttpResponse response) {
if (response.code == HttpResponse.HTTP_OK) {
System.out.println(response.toJSONObject());
}
}
});
request.setOnErrorListener(new HttpRequest.OnErrorListener() {
@Override
public void onError(HttpError error) {
// There was an error, deal with it
}
});
JSONObject json;
try {
json = new JSONObject();
json.put("foo", "bar");
json.put("jane", "doe");
} catch (JSONException ignore) {
return;
}
request.post("https://httpbin.org/post", json);
```
### PUT, PATCH and DELETE
These methods are also supported and expected the same set of parameters as POST. The convenience methods are
request.put();
request.patch();
request.delete();
### Custom Headers
Headers can be sent using the `HttpHeaders` class.
```java
HttpHeaders headers = new HttpHeaders("Authorization", "Token sasdasdai2sadas")
request.get("https://httpbin.org/get", headers);
```
### FormData
```java
FormData data = new FormData();
data.put("foo", "bar");
data.put("file2", new File("/sdcard/image.png"));
request.post("https://httpbin.org/post", data);
```