Giter Site home page Giter Site logo

msoftware / optimushttp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nisrulz/optimushttp

0.0 2.0 0.0 62 KB

Android library that simplifies networking in android via an async http client

Home Page: http://nisrulz.github.io/OptimusHTTP

License: Apache License 2.0

Java 100.00%

optimushttp's Introduction

#OptimusHTTP Maven Central Android Arsenal Twitter

Simplified android async HTTP client

#Integration

  • OptimusHTTP is available in the MavenCentral, so getting it is as simple as adding it as a dependency
compile 'com.github.nisrulz:optimushttp:1.0.0'

#Usage

  • Setup your SERVER url
// Link obtained from : http://requestb.in/
String SERVER = "http://requestb.in/168uy1z1";
  • Create an instance of the OptimusHTTP class
 OptimusHTTP client = new OptimusHTTP();
  • Next if in debug stage, enable logs
client.enableDebugging();
  • Define parameters to be sent with the request
ArrayMap<String, String> params = new ArrayMap<>();
params.put("email", "[email protected]");
params.put("pass", "abc");
  • Define configurations for the request
  • Type of Method : POST/GET
    • POST Request
client.setMethod(OptimusHTTP.METHOD_POST);
 + GET Request
client.setMethod(OptimusHTTP.METHOD_GET);
  • Type of Mode : PARALLEL/SEQ
    • Parallel Request
client.setMode(OptimusHTTP.MODE_PARALLEL);
 + Sequential Request
client.setMode(OptimusHTTP.MODE_SEQ);
  • To make a request create an object of HttpReq class.

The client.makeRequest() function returns reference to each HttpReq object created which you can save in an ArrayList and then later on call cancel function on them to cancel the requests

```java

ArrayList<HttpReq> refHttpReqList = new ArrayList<>();
try {
     // makeRequest() returns the reference of the request made
     // which can be used later to call the cancelReq() if required
     // if no request is made the makeRequest() returns null
    HttpReq req = client.makeRequest(MainActivity.this, SERVER, params, responseListener);
        if (req != null)
            refHttpReqList.add(req);
} catch (Exception e) {
        e.printStackTrace();
}
```
  • To cancel one requests
client.cancelReq(req);
  • To cancel all requests
if (refHttpReqList.size() > 0) {
    for (int i = 0; i < refHttpReqList.size(); i++)
        client.cancelReq(refHttpReqList.get(i));
    refHttpReqList.clear();
}
  • Implement the Callback
// Listener for the Response received from server
private final OptimusHTTP.ResponseListener responseListener = new OptimusHTTP.ResponseListener() {
        @Override
        public void onSuccess(String msg) {
            System.out.println(msg);
        }

        @Override
        public void onFailure(String msg) {
            System.out.println(msg);
        }
    };

License

Copyright 2016 Nishant Srivastava

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

optimushttp's People

Contributors

nisrulz avatar

Watchers

Michael jentsch 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.