Skip to content

A very simple android networking library. It is built entirely in kotlin. Internals are not complex for this library, it simply uses HttpURLConnnection. But it does proper thread management. Just make request and listen for the response.

Notifications You must be signed in to change notification settings

droid-ash/Simple-Networking

Repository files navigation

Simple-Networking

About The Project

A very simple android networking library. It is built entirely in kotlin. Internals are not complex for this library, it simply uses HttpURLConnnection. But it does proper thread management. Just make request and listen for the response.
This project was part of my first article on medium. Here is the article link

Requirements

Simple android networking library supports Android 5.0 (Lollipop) and later.

Getting Started

Add this in your app/build.gradle

implementation 'com.simple.networking:http:0.1.0'

Add this in your project/build.gradle

repositories {
    maven {
        url "https://simple-android-libs.bintray.com/SimpleAndroidNetworking"
    }
}

Making a GET Request (JSONArray Response)

        Http.Request(Http.GET)
            .url("https://jsonplaceholder.typicode.com/users")
            .enableLog(true)
            .execute(object : JSONArrayListener {
                override fun onResponse(res: JSONArray?) {

                }

                override fun onFailure(e: Exception?) {
                    
                }
            })              

Making a GET Request (JSONObject Response)

        Http.Request(Http.GET)
            .url("https://jsonplaceholder.typicode.com/users/1")
            .enableLog(true)
            .execute(object : JSONObjectListener {
                override fun onResponse(res: JSONObject?) {

                }

                override fun onFailure(e: Exception?) {

                }
            })

Making a Request with header

        Http.Request(Http.GET)
            .url("https://jsonplaceholder.typicode.com/users")
            .header("k1", "v1")
            .header("k2", "v2")
            .enableLog(true)
            .execute(object : JSONArrayListener {
                override fun onResponse(res: JSONArray?) {
                    Log.d("MainActivity", res.toString())
                }

                override fun onFailure(e: Exception?) {
                    Log.d("MainActivity", e.toString())
                }
            })

Making a Request with header (or using map)

        val headerMap: HashMap<String, String> = HashMap()
        headerMap["k1"] = "v1"
        headerMap["k2"] = "v2"
        Http.Request(Http.GET)
            .url("https://jsonplaceholder.typicode.com/users")
            .header(headerMap)
            .enableLog(true)
            .execute(object : JSONArrayListener {
                override fun onResponse(res: JSONArray?) {
                    Log.d("MainActivity", res.toString())
                }

                override fun onFailure(e: Exception?) {
                    Log.d("MainActivity", e.toString())
                }
            })

Making a POST Request

        val js = JSONObject()
        js.put("name", "ashish")
        Http.Request(Http.POST)
            .url("https://jsonplaceholder.typicode.com/users")
            .body(js)
            .enableLog(true)
            .execute(object : JSONObjectListener {
                override fun onResponse(res: JSONObject?) {

                }

                override fun onFailure(e: Exception?) {

                }
            })

Making a PUT Request

        val js = JSONObject()
        js.put("name", "ashish")
        Http.Request(Http.PUT)
            .url("https://jsonplaceholder.typicode.com/users")
            .body(js)
            .enableLog(true)
            .execute(object : JSONObjectListener {
                override fun onResponse(res: JSONObject?) {
                    
                }

                override fun onFailure(e: Exception?) {

                }
            })

Making a DELETE Request

        val js = JSONObject()
        js.put("name", "ashish")
        Http.Request(Http.DELETE)
            .url("https://jsonplaceholder.typicode.com/users")
            .body(js)
            .enableLog(true)
            .execute(object : JSONObjectListener {
                override fun onResponse(res: JSONObject?) {

                }

                override fun onFailure(e: Exception?) {

                }
            })

Contact - Let's become friend

About

A very simple android networking library. It is built entirely in kotlin. Internals are not complex for this library, it simply uses HttpURLConnnection. But it does proper thread management. Just make request and listen for the response.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages