GithubHelp home page GithubHelp logo

izzyalonso / http-requests Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sandwatch/http-requests

0.0 1.0 0.0 193 KB

An Android library to make HTTP requests.

License: Apache License 2.0

Java 100.00%

http-requests's Introduction

HTTP-Requests

An Android library to perform HTTP requests in a simple fashion.

Importing the library

compile 'es.sandwatch:http-requests:1.0.0'

Using the library

Initialization

In you Application class' onCreate() method, include this line to initialize the library:

HttpRequest.init(getApplicationContext());

Additionaly, you can set permanent headers and url parameters:

HttpRequest.addHeader("Authorization", "header content");
HttpRequest.addUrlParameter("app_version", "1.0.0");

These parameters and headers will be sent with every request until you decide to remove them:

HttpRequest.removeHeader("Authorization");
HttpRequest.removeUrlParameter("app_version");

By default, the request response is delivered in UTF-8 format, but you can change this behavior using the following method:

HttpRequest.setEncoding("A valid encoding name");

Finally, some of the parameters you can tweak are the request timeout, the number of retries, and the retry backoff.

HttpRequest.setRequestTimeout(20000);
HttpRequest.setRequestRetries(4);
HttpRequest.setRequestBackoff(2);

The default values are 10 seconds for the request timeout, no retries, and a backoff of 1.5 of the previous attempt's timeout.

Making requests

Making requests with this library is simple. Once you initialized the HttpRequest class, you can use the get(), post(), put(), or delete() methods to make a request. The first argument all the methods is an instance of the HttpRequest.RequestCallback interface. Except for the methods associated with GET, this parameter is optional, since you may not care about the server response, if you don't wish to provide a callback pass null. The second parameter of these functions is a URL, which is mandatory. The third parameter is, in the case of the methods associated with POST and PUT, the request body, passed as a JSONObject.

When calling any of the request-performing methods of the class, a request code will be issued. This request code will sere you to identify particular requests later on, when the callback is called, if you are making your particular activity, fragment, adapter, or else implement HttpRequests.RequestCallback. Here is a sample activity that performs four different requests:

public class RequestActivity extends AppCompatActivity implements HttpRequest.RequestCallback{
    private int mGetRequest;
    private int mPostRequest;
    private int mPutRequest;
    private int mDeleteRequest;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        
        //Set layout, grab UI components, etc... skipped in this example
        
        JSONObject postBody = new JSONObject();
        JSONObject putBody = new JSONObject();
        //Add parameters to the json objects
        
        //Make the requests
        mGetRequest = HttpRequest.get(this, "https://...");
        mPostRequest = HttpRequest.post(this, "https://...", postBody);
        mPutRequest = HttpRequest.put(this, "https://...", putBody);
        mDeleteRequest = HttpRequest.delete(this, "https://...");
    }
    
    @Override
    public void onRequestComplete(int requestCode, String result){
        if (requestCode == mGetRequest){
            ...
        }
        else if (requestCode == mPostRequest){
            ...
        }
        else if (requestCode == mPutRequest){
            ...
        }
        else if (requestCode == mDeleteRequest){
            ...
        }
    }
    
    @Override
    public void onRequestFailed(int requestCode, HttpRequestError error){
        if (requestCode == mGetRequest){
            ...
        }
        else if (requestCode == mPostRequest){
            ...
        }
        else if (requestCode == mPutRequest){
            ...
        }
        else if (requestCode == mDeleteRequest){
            ...
        }
    }
}

http-requests's People

Contributors

izzyalonso avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.