https://github.com/anitaa1990/requesttask
Android library wrapper for HttpUrlConnection
https://github.com/anitaa1990/requesttask
Last synced: 4 months ago
JSON representation
Android library wrapper for HttpUrlConnection
- Host: GitHub
- URL: https://github.com/anitaa1990/requesttask
- Owner: anitaa1990
- License: apache-2.0
- Created: 2016-10-04T05:06:26.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-10-28T16:59:39.000Z (almost 9 years ago)
- Last Synced: 2025-04-12T05:35:37.958Z (6 months ago)
- Language: Java
- Homepage: https://github.com/anitaa1990/RequestTask
- Size: 94.7 KB
- Stars: 5
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: license.txt
Awesome Lists containing this project
README
# RequestTask
This is a simple wrapper for HttpUrlConnection. Available in for Java and Android.
You can use the RequestTask class in any of your android projects. The wrapper is just a single class.
Wrappers for
HTTP GET
HTTP POST
HTTP PUT
HTTP DELETEUsage:
```RequestTask requestTask = new RequestTask();```HTTP GET:
```
requestTask.get("sample_one.json", new RequestTask.TaskResponseListener() {
@Override
public void onSuccess(Object extra) {
System.out.println("Successful " + extra);
}@Override
public void onError(RequestError error) {
System.out.println("Error " + error.getHeaders());
System.out.println("Error " + error.getResponseCode());
System.out.println("Error " + error.getResponseMessage());
}
});
```
HTTP POST:```
requestTask.post("sample_one.json", jsonObject, new RequestTask.TaskResponseListener() {
@Override
public void onSuccess(Object extra) {
System.out.println("Successful " + extra);
}@Override
public void onError(RequestError error) {
System.out.println("Error " + error.getHeaders());
System.out.println("Error " + error.getResponseCode());
System.out.println("Error " + error.getResponseMessage());
}
});
```
HTTP PUT:```
requestTask.put("sample_one.json", new RequestTask.TaskResponseListener() {
@Override
public void onSuccess(Object extra) {
System.out.println("Successful " + extra);
}@Override
public void onError(RequestError error) {
System.out.println("Error " + error.getHeaders());
System.out.println("Error " + error.getResponseCode());
System.out.println("Error " + error.getResponseMessage());
}
});
```
HTTP DELETE:```
requestTask.delete("sample_one.json", new RequestTask.TaskResponseListener() {
@Override
public void onSuccess(Object extra) {
System.out.println("Successful " + extra);
}@Override
public void onError(RequestError error) {
System.out.println("Error " + error.getHeaders());
System.out.println("Error " + error.getResponseCode());
System.out.println("Error " + error.getResponseMessage());
}
});
```You need to call the below method to start the async Request Task:
```
requestTask.start();
```Note: You need to pass the Base url to BASE_URL variable in RquestTask.java class
And that's it!Wrapper to store in Local Cache
If you would like to save your request response to a local cache, you can use the ObjectUtil.java class:
You cal access this class using:
```
ObjectUtil objDataStream = new ObjectUtil();
objDataStream.writeObjects(object,fileName);
```You need to provide a fileName here.
This filename needs to be the path where you would like to store this object.
The object could be anything.In order to read the file from the local cache, you can use:
```
ObjectUtil objDataStream = new ObjectUtil();
return objDataStream.readObjects(fileName);
```Or you could use the Util class provided in the library and use:
```
RequestUtil.writeObjectToDisk(fileName, result);
RequestUtil.readObjectFromDisk(result);
```Enjoy!