An open API service indexing awesome lists of open source software.

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

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);
```