OptimusHTTP

Additional

Language
Java
Version
1.2.1 (Oct 22, 2016)
Created
Nov 16, 2015
Updated
Jul 25, 2017
Owner
Nishant Srivastava (nisrulz)
Contributor
Nishant Srivastava (nisrulz)
1
Activity
Badge
Generate
Download
Source code
APK file

Announcement

Specs

Badges/Featured In

Also included in

Show some ❤️

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

Including in your project

OptimusHTTP is available in the Jcenter, so getting it as simple as adding it as a dependency

compile 'com.github.nisrulz:optimushttp:{latest version}'

where {latest version} corresponds to published version in

#Usage

  • Setup your SERVER url
String SERVER = "http://uinames.com/api/";
  • Create an instance of the OptimusHTTP class
 OptimusHTTP client = new OptimusHTTP(context);
  • 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", "abc@abc.com");
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);
    • PUT Request
    client.setMethod(OptimusHTTP.METHOD_PUT);
    • DELETE Request
    client.setMethod(OptimusHTTP.METHOD_DELETE);
  • Type of Mode : PARALLEL/SEQ

    • Parallel Request
    client.setMode(OptimusHTTP.MODE_PARALLEL);
    • Sequential Request
    client.setMode(OptimusHTTP.MODE_SEQ);
  • Setup timeout values (optional, default is 10s)

    • Connect Timeout
    client.setConnectTimeout(10 * 1000);
    • Read Timeout
    client.setReadTimeout(10 * 1000);
  • Setup content type (optional, deafult is CONTENT_TYPE_FORM_URL_ENCODED)

    client.setContentType(OptimusHTTP.CONTENT_TYPE_JSON);

    Available Types

    + `OptimusHTTP.CONTENT_TYPE_FORM_URL_ENCODED`
    + `OptimusHTTP.CONTENT_TYPE_JSON`
    + `OptimusHTTP.CONTENT_TYPE_PDF`
    + `OptimusHTTP.CONTENT_TYPE_HTML`
    + `OptimusHTTP.CONTENT_TYPE_IMG_PNG`
    + `OptimusHTTP.CONTENT_TYPE_TEXT`
    
  • Setup Headers (optional)

    ArrayMap<String, String> headerMap = new ArrayMap<>();
    headerMap.put("Accept-Encoding", "gzip, deflate");
    headerMap.put("Accept-Language", "en-US");
    headerMap.put("Content-Language", "en-US");
    client.setHeaderMap(headerMap);
  • 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(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);
        }
    };

Pull Requests

I welcome and encourage all pull requests. It usually will take me within 24-48 hours to respond to any issue or request. Here are some basic rules to follow to ensure timely addition of your request:

  1. Match coding style (braces, spacing, etc.) This is best achieved using CMD+ Option+ L (Reformat code) on Mac (not sure for Windows) with Android Studio defaults.
  2. If its a feature, bugfix, or anything please only change code to what you specify.
  3. Please keep PR titles easy to read and descriptive of changes, this will make them easier to merge :)
  4. Pull requests must be made against develop branch. Any other branch (unless specified by the maintainers) will get rejected.
  5. Check for existing issues first, before filing an issue.
  6. Have fun!

Created & Maintained By

Nishant Srivastava ( @nisrulz)

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.