:satellite: [Android Library] Simplified async networking in android

Overview

Image

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

Also featured in [Awesome Android Newsletter #Issue 15 ]
Built with ❤︎ by Nishant Srivastava and contributors


Including in your project

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

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

where {latest version} corresponds to published version in Download

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", "[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);
    • 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);
        }
    };

Screenshots

sc1

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. This project uses a modified version of Grandcentrix's code style, so please use the same when editing this project.
  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!

License

Licensed under the Apache License, Version 2.0, click here for the full license.

Author & support

This project was created by Nishant Srivastava but hopefully developed and maintained by many others. See the the list of contributors here.

If you appreciate my work, consider buying me a cup of to keep me recharged 🤘

  • PayPal
  • Bitcoin Address: 13PjuJcfVW2Ad81fawqwLtku4bZLv1AxCL

I love using my work and I'm available for contract work. Freelancing helps to maintain and keep my open source projects up to date!

You might also like...
Latihan Networking dengan Retrofit
Latihan Networking dengan Retrofit

Latihan-Background-Process-dan-Networking-9 Latihan Networking dengan Retrofit Pada materi kali ini Anda akan belajar menggunakan Retrofit untuk menam

Repo of the Open Source Android library : RoboSpice. RoboSpice is a modular android library that makes writing asynchronous long running tasks easy. It is specialized in network requests, supports caching and offers REST requests out-of-the box using extension modules.
Android network client based on Cronet. This library let you easily use QUIC protocol in your Android projects

Android network client based on Cronet. This library let you easily use QUIC protocol in your Android projects

Android Easy Http - Simplest android http request library.
Android Easy Http - Simplest android http request library.

Android Easy Http Library 繁體中文文檔 About Android Easy Http Library Made on OkHttp. Easy to do http request, just make request and listen for the respons

dns library for android

Qiniu Happy DNS for Android 安装 直接安装 通过maven 使用方法 DnsManager 可以创建一次,一直使用。 IResolver[] resolvers = new IResolver[3]; resolvers[0] = AndroidDnsSe

Asynchronous socket, http(s) (client+server) and websocket library for android. Based on nio, not threads.

AndroidAsync AndroidAsync is a low level network protocol library. If you are looking for an easy to use, higher level, Android aware, http request li

The best file downloader library for Android
The best file downloader library for Android

Overview Fetch is a simple, powerful, customizable file download manager library for Android. Features Simple and easy to use API. Continuous download

Android library listening network connection state and change of the WiFi signal strength with event bus

NetworkEvents Android library listening network connection state and change of the WiFi signal strength with event bus. It works with any implementati

android lightweight graphql library

android lightweight graphql library

Comments
  • showing toast when failure happen, not works

    showing toast when failure happen, not works

    hai again.. :)

    why when i type : Toast.makeText(MainActivity.this, "NOT WORK", Toast.LENGTH_SHORT).show(); on failure method it would make my app error ? error msg : " Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()"

    thanks

    opened by navotera 2
  • Migrate to Maven Central

    Migrate to Maven Central

    JFrog just announced that they'll be shutting down JCenter: https://jfrog.com/blog/into-the-sunset-bintray-jcenter-gocenter-and-chartcenter/

    Key dates are:

    February 28th: No more submissions will be accepted to Bintray, JCenter May 1st: Bintray, JCenter services will no longer be available

    Hence need to migrate to another maven repository like Maven central.

    opened by nisrulz 0
  • Pass a raw body instead of an array of parameters

    Pass a raw body instead of an array of parameters

    Would it be possible to pass a Raw body in a POST request as an alternative to the array params?

    Right now, if I try to pass it like this:

    params.put("", my_raw_string);
    

    If get a 400 Bad Request from the requested server.

    Thank you!

    opened by mmethot 0
  • Send only json in post request

    Send only json in post request

    How can I send only json in post request?

    I'm using ArrayMap<String, String> params = new ArrayMap<String, String>(); String jsonParam = gson.toJson(param); params.put("", jsonParam); But the request is send with = symbol Example: ={test:"test"}

    I already put the OptimusHttp.CONTENT_TYPE_JSON, but the "=" still appear

    enhancement Hacktoberfest 
    opened by dielfilho-zz 3
Releases(1.2.5)
Owner
Nishant Srivastava
non-GDE Android Engineer crushing code 👨🏻‍💻
Nishant Srivastava
Unirest in Java: Simplified, lightweight HTTP client library.

Unirest for Java Install With Maven: <!-- Pull in as a traditional dependency --> <dependency> <groupId>com.konghq</groupId> <artifactId>unire

Kong 2.4k Dec 24, 2022
Async file downloader for Android

What is FileBox FileBox is an async file downloader library for Android. Before we develop filebox, we though that URL content changes very rarely. So

Lyrebird Studio 311 Dec 25, 2022
🚀 A Complete Fast Android Networking Library that also supports HTTP/2 🚀

Fast Android Networking Library About Fast Android Networking Library Fast Android Networking Library is a powerful library for doing any type of netw

AMIT SHEKHAR 5.5k Dec 27, 2022
The easiest HTTP networking library for Kotlin/Android

Fuel The easiest HTTP networking library for Kotlin/Android. You are looking at the documentation for 2.x.y.. If you are looking for the documentation

Kittinun Vantasin 4.3k Jan 8, 2023
🚀 A Complete Fast Android Networking Library that also supports HTTP/2 🚀

Fast Android Networking Library About Fast Android Networking Library Fast Android Networking Library is a powerful library for doing any type of netw

AMIT SHEKHAR 5.5k Jan 3, 2023
Flower - Super cool Android library to manage networking and database caching with ease

Flower Super cool Android library to manage networking and database caching with ease. It allows developers to use remote resources on-the-fly OR Comb

Rajesh Hadiya 192 Dec 26, 2022
Volley is an HTTP library that makes networking for Android apps easier and, most importantly, faster.

Volley Volley is an HTTP library that makes networking for Android apps easier and, most importantly, faster. For more information about Volley and ho

Google 3.3k Jan 1, 2023
Kotlin-echo-client - Echo client using Kotlin with Ktor networking library

Overview This repository contains an echo server implemented with Kotlin and kto

Elliot Barlas 2 Sep 1, 2022
Android Asynchronous Networking and Image Loading

Android Asynchronous Networking and Image Loading Download Maven Git Features Kotlin coroutine/suspend support Asynchronously download: Images into Im

Koushik Dutta 6.3k Dec 27, 2022
IceNet - Fast, Simple and Easy Networking for Android

IceNet FAST, SIMPLE, EASY This library is an Android networking wrapper consisting of a combination of Volley, OkHttp and Gson. For more information s

Anton Nurdin Tuhadiansyah 61 Jun 24, 2022