Giter Club home page Giter Club logo

minimaljavanetworking's Introduction

Minimal Java Networking

with token-based Authorization support

This is the code which I use to connect to the server in my Android apps, factored out in a library. It doesn't provide any caching (beyond HttpURLConnection default) nor, well, anything. It is meant to easily and reliably make connections and communicate with an external service (i.e. REST API)

By default, all requests are made on a background thread, either in a blocking or async fashion.

For usage instructions, start with NetworkRequestBuilder. It contains all possible methods to call and make a request.

Examples

Making a request:

NetworkRequestBuilder.create(new URL("http://example.com"), VERB_GET)
                     .id(requestId)
                     .handler(exceptionHandler)
                     .async(callbacks);

Or, if uploading a file:

File zipFile = new File("example.zip");
NetworkRequestBuilder.create(new URL("http://example.com"), VERB_POST, zipFile)
                     .id(requestId)
                     .auth(TokenManager.getInstance(c))
                     .handler(exceptionHandler)
                     .executor(executor)
                     .async(callbacks);

There are multiple #create(...) methods which are part of the NetworkRequestBuilder. If not passing a file to the method, it is implied that you're sending a Map<String, String> as request parameters (possibly Network.emptyMap) and receiving a String. If you're passing a file and parameter map, it is implied that you're sending parameters and saving response to a file. If you're only passing a File, it is implied that you're sending a File and receiving a String. If you need less opinionated framework, you can use Network.Request and Network.Response directly.

Implementing callbacks:

public void onRequestCompleted(int requestId, Network.Response<String> response) {
    switch (requestId) {
        case REQUEST_DATA:
            if(response.responseCode == RESPONSE_OK) {
                //parse response.responseData
            }
            //if not using NetworkExceptionHandler, need to handle other response codes here as well
            //otherwise, appropriate method from NEH has already been called
            break;
        case REQUEST_OTHER_DATA:
            if(response.responseCode == RESPONSE_OK) {
                //parse response.responseData
            }
            break;
        default:
            throw new UnsupportedOperationException("invalid request id!");
    }
}

Example implementation of NetworkExceptionHandler is given inside the source file; as it was written for Android, it requires Android SDK and has been commented out.

minimaljavanetworking's People

Contributors

luka-j avatar

Watchers

 avatar  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.