Skip to content

anitaa1990/RequestTask

Repository files navigation

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 DELETE

Usage:

```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!

About

Android library wrapper for HttpUrlConnection

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages