https://github.com/101loop/drf-android
Android Integration for Django REST Framework
https://github.com/101loop/drf-android
Last synced: 10 months ago
JSON representation
Android Integration for Django REST Framework
- Host: GitHub
- URL: https://github.com/101loop/drf-android
- Owner: 101Loop
- License: apache-2.0
- Created: 2018-12-23T04:49:19.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2020-09-27T04:22:44.000Z (over 5 years ago)
- Last Synced: 2025-03-30T17:11:13.461Z (11 months ago)
- Language: Java
- Size: 160 KB
- Stars: 7
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Django-REST-Framework---Android
Android Integration for Django REST Framework
[  ](https://bintray.com/civilmachines/Django-REST-Framework/DRFAPI/0.0.2/link)
## Gradle
In your module (app-level) Gradle file (usually `app/build.gradle`), Add this
line inside the `dependencies` block.
```java
dependencies {
...
implementation 'com.civilmachines.drfapi:drfapi:0.0.2'
}
```
## Usage
Register your Volley `SingletonFile` to your `AndroidManifest.xml`. You can copy
the sample code from
[VolleySingleton Gist](https://gist.github.com/iamvivekkaushik/b0608ff18902696051856c41f3e7e332)
```xml
...
```
## JAVA
For JSONObject, you can use `DjangoJSONObjectRequest`
```java
DjangoJSONObjectRequest request = new DjangoJSONObjectRequest(Request.Method.POST, "url", requestData,
new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
// Response Received
}
}, new DjangoErrorListener() {
// Override Methods here
@Override
public void defaultErrorListener(String message) {
super.defaultErrorListener(message);
}
}, this);
request.setRetryPolicy(new DefaultRetryPolicy(0, -1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
VolleySingleton.getInstance(this).getRequestQueue().add(request);
```
For JSONArray, you can use `DjangoJSONArrayResponseRequest`
```java
DjangoJSONArrayResponseRequest request = new DjangoJSONArrayResponseRequest(Request.Method.GET, "url", null,
new Response.Listener() {
@Override
public void onResponse(JSONArray response) {
// Response Received
}
}, new DjangoErrorListener() {
// Override Methods here
@Override
public void defaultErrorListener(String message) {
super.defaultErrorListener(message);
}
}, this);
request.setRetryPolicy(new DefaultRetryPolicy(0, -1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
VolleySingleton.getInstance(this).getRequestQueue().add(request);
```
If you need to pass some data with request, you can create a JSONObject and pass
it in the request.
```java
JSONObject requestData = new JSONObject();
requestData.put("key", "value");
```
This library uses shared preferences for Authentication Token, just save your
token using the below code and it will be set to header automatically.
```java
UserSharedPreferenceAdapter usrAdapter = new UserSharedPreferenceAdapter(this);
usrAdapter.saveToken("token");
// Use the get token method to retrieve the token
String token = usrAdapter.getToken();
```
## Author
- [Himanshu Shankar](https://himanshus.com)