Coder Social home page Coder Social logo

requestsj's Introduction

RequestsJ

Requests Java (Android, really)

DISCLAIMER: INSPIRED BY BUT NOT ASSOCIATED WITH https://github.com/psf/requests

Getting started

The build is hosted on jcenter so you could just add the line:

implementation 'pk.codebase.requests:requests:0.6'

Usage

The API is written for simplicity, below examples provide a peak into it.

GET

HttpRequest request = new HttpRequest();
request.setOnResponseListener(new HttpRequest.OnResponseListener() {
    @Override
    public void onResponse(HttpResponse response) {
        if (response.code == HttpResponse.HTTP_OK) {
            System.out.println(response.toJSONObject());
        }
    }
});
request.setOnErrorListener(new HttpRequest.OnErrorListener() {
    @Override
    public void onError(HttpError error) {
        // There was an error, deal with it
    }
});
request.get("https://httpbin.org/get");

POST

HttpRequest request = new HttpRequest();
request.setOnResponseListener(new HttpRequest.OnResponseListener() {
    @Override
    public void onResponse(HttpResponse response) {
        if (response.code == HttpResponse.HTTP_OK) {
            System.out.println(response.toJSONObject());
        }
    }
});
request.setOnErrorListener(new HttpRequest.OnErrorListener() {
    @Override
    public void onError(HttpError error) {
        // There was an error, deal with it
    }
});

JSONObject json;
try {
    json = new JSONObject();
    json.put("foo", "bar");
    json.put("jane", "doe");
} catch (JSONException ignore) {
    return;
}
request.post("https://httpbin.org/post", json);

PUT, PATCH and DELETE

These methods are also supported and expected the same set of parameters as POST. The convenience methods are request.put(); request.patch(); request.delete();

Custom Headers

Headers can be sent using the HttpHeaders class.

HttpHeaders headers = new HttpHeaders("Authorization", "Token sasdasdai2sadas")
request.get("https://httpbin.org/get", headers);

FormData

FormData data = new FormData();
data.put("foo", "bar");
data.put("file2", new File("/sdcard/image.png"));
request.post("https://httpbin.org/post", data);

requestsj's People

Contributors

om26er avatar surc-bot avatar s9iper1 avatar

Stargazers

Shahid Ghafoor avatar Mohsin Falak avatar

Watchers

James Cloos avatar  avatar  avatar husnain kareem avatar Shahid Ghafoor avatar Muhammad Imran avatar Muzzammil Shahid avatar

requestsj's Issues

Add CompletableFuture based API option

We should support Java8' CompletableFuture. In Android it's only available on API level 24+. It will enable us to provide an alternate to the current callbacks approach.

NB: An issue for providing CompletableFuture for older versions of Android exists with upstream https://issuetracker.google.com/issues/153794291. With Android Studio 4.0 the new desugar support was improved and we may eventually see the upstream embrace it.

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.