NetworkConnection is a simple library to http request support.

Overview

Maven Central

NetworkConnection Maven Central

Overview

NetworkConnection is a simple library to http request support. this mean goal is to provide a very simple interface to developer.

Gradle

compile 'com.github.rudda:networkconnetion:0.0.1'

Permissions

add line in android manifest file

">
<uses-permission android:name="android.permission.INTERNET">uses-permission>

Simple GET Request -with Return JSONArray

1º step:

you must implements JsonArrayListener interface
public class MainActivity extends AppCompatActivity implements JsonArrayListener 

2º step:

you must to instance customRequest class and set attribuites base url ( **setBaseUrl** ) , router ( **setRouter** ) and the method to request ( **GET or POST** ) this example GET.
  CustomRequest customRequest = new CustomRequest();
        customRequest.setBaseUrl("http://localhost/users"); //base url
        customRequest.setRouter("/"); //router to REST API
        customRequest.setMethod(Request.Method.GET);// methods ( get or post )
        customRequest.setResult(0); //optional

        NetworkConnection.getInstance(this).execute(this, customRequest);

3º step:

the return.
the return can come in three states: success, fail and loading. The first state is always loading the second state can be success or fail. warning: you can also use the requestCode to represent a specific call.
  @Override
    public void sucess(JSONArray Jsonarray, int requestCode) {

        Toast.makeText(this, "sucess", Toast.LENGTH_SHORT).show();
        TextView tv = (TextView) findViewById(R.id.response);

        tv.setText(Jsonarray.toString());


    }

    @Override
    public void fail(String message, int requestCode) {

        Toast.makeText(this, "falha", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void loading(boolean loading, int requestCode) {
        Toast.makeText(this, "carregando...", Toast.LENGTH_SHORT).show();

    }

add headers

you must instantiate a hashmap of type add key and respective values. Then just add the hashmap to the setHeader method of an instance of the CustomRequest class. the example below shows how to add an authorization header.

 HashMap<String, String> params = new HashMap<>();
               params.put("Authorization", "Basic " + CustomRequest.generateAutorizationHeader(email, pass));
               customRequest.setHeaders(params);
          

working with JsonObject or String

how to work JsonObject or String? Easy :) ... do you must to implement JSONObjectListener and/or StringListener interface as shown below.

public class MainActivity extends AppCompatActivity implements  StringListener{

  
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomRequest customRequest = new CustomRequest();
        customRequest.setBaseUrl("http://localhost/users"); //base url
        customRequest.setRouter("/"); //router to REST API
        customRequest.setMethod(Request.Method.GET);// methods ( get or post )
        customRequest.setResult(0); //optional

        NetworkConnection.getInstance(this).execute(this, customRequest);


    }

    public void fail(String message, int requestCode) {

       
    }

    @Override
    public void loading(boolean loading, int requestCode) {
      
    }

    @Override
    public void sucess(String string, int requestCode) {

    }

}

OR

public class MainActivity extends AppCompatActivity implements  JSONObjectListener{

  
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        CustomRequest customRequest = new CustomRequest();
        customRequest.setBaseUrl("http://localhost/users"); //base url
        customRequest.setRouter("/"); //router to REST API
        customRequest.setMethod(Request.Method.GET);// methods ( get or post )
        customRequest.setResult(0); //optional

        NetworkConnection.getInstance(this).execute(this, customRequest);


    }

    public void fail(String message, int requestCode) {

       
    }

    @Override
    public void loading(boolean loading, int requestCode) {
      
    }

     @Override
    public void sucess(JSONObject jsonObject, int requestCode) {

    }

}

Simple Post Request

  CustomRequest customRequest = new CustomRequest();
        customRequest.setBaseUrl("http://localhost/users"); //base url
        customRequest.setRouter("/"); //router to REST API
        customRequest.setMethod(Request.Method.POST);// methods ( get or post )
        customRequest.setResult(0); //optional

        NetworkConnection.getInstance(this).execute(this, customRequest);

add params to Post Request

params= new HashMap<>(); params.put("param_1", "value_1"); params.put("param_2", "value_2"); params.put("param_3", "value_3"); customRequest.setParams(params); //only POST NetworkConnection.getInstance(this).execute(this, customRequest);">
  
  CustomRequest customRequest = new CustomRequest();
        customRequest.setBaseUrl("http://localhost/users"); //base url
        customRequest.setRouter("/"); //router to REST API
        customRequest.setMethod(Request.Method.POST);// methods ( get or post )
        customRequest.setResult(0); //optional

         HashMap<String, String> params= new HashMap<>();
                params.put("param_1", "value_1");
                params.put("param_2", "value_2");
                params.put("param_3", "value_3");
        
          customRequest.setParams(params); //only POST
        
        NetworkConnection.getInstance(this).execute(this, customRequest);

acknowledgment

agradecimento a contribuição direta/indireta de thiengo calopsita por meio do canal Vinicios Thiengo e do seu blog https://www.thiengo.com.br/

You might also like...
Spring-graphql-getting-started - Spring for GraphQL provides support for Spring applications built on GraphQL Java

Getting Started with GraphQL and Spring Boot Spring for GraphQL provides support

Open-Source Forge 1.8.9 Hypixel Duels bot! Planned to support many modes and written in Kotlin.
Open-Source Forge 1.8.9 Hypixel Duels bot! Planned to support many modes and written in Kotlin.

This project has been moved to a new repository: [HumanDuck23/duck-dueller-v2](https://github.com/HumanDuck23/duck-dueller-v2) Duck Dueller Are you ti

Simple event library to communicate between Activity/Fragment and ViewModel

Setup dependencies { implementation "com.github.skgmn:viewmodelevent:1.1.0" } If you don't know how to access to GitHub Packges, please refer to

A Simple Android library to get the number of words and give you the time it will take you to finish an article/story.

MinRead A Simple Android library to get the number of words and give you the time it will take you to finish an article/story. Prerequisite Androidx K

Simple Kotlin localization library.

Simple Kotlin localization library.

🌨️ Simple, intuitive, and opinionated command handling library for Kord

🌨️ Snow Simple, intuitive, and opinionated command handling library for Kord Why? Since I maintain two Discord bots, both in Kotlin, Nino and Noel (p

Kbackend - A simple backend library for creating backends in Kotlin/Java

kbackend A simple backend library for creating backends in Kotlin/Java Setup Thi

Kotlin multi-platform simple File I/O library

KmpIO This is a Kotlin multiplatform (KMP) library for basic Text file, Binary file, and zip/archive file IO. It was initially implemented with the an

Web Container: A simple web container library for Android to help fellow developer to open WebView easily

WebContainer Description Web Container is a simple web container library for And

Releases(0.0.1)
Owner
rudda
rudda
A demo project which demonstrates the work of javax.servlet.Filter capable of escaping / modifying / removing a part of JSON request based on specified criteria.

Replace Filter Demo A demo project which demonstrates the work of javax.servlet.Filter capable of escaping / modifying / removing a part of JSON reque

Vlad Krava 1 Jan 17, 2022
🪟 Pluggable Ktor plugin to implement Sentry for error handling and request contexts

?? Ktor Plugin for Sentry Pluggable Ktor plugin to implement Sentry for error handling and request contexts. What is this library? This basically impl

Noel 3 Dec 6, 2022
Repository with source code from http://rosettacode.org/wiki/Category:Kotlin

Rosetta Code Kotlin This is a repository with the Kotlin source code from RosettaCode wiki. The main motivation for extracting all the code into a rep

Dmitry Kandalov 20 Dec 27, 2022
Provides Ktor Server libs for building awesome Kotlin plugins which needs to provide builtin HTTP servers

Ktor Plugin Provides Ktor Server libs for building awesome Kotlin plugins which needs to provide builtin HTTP servers. Requires: https://github.com/Po

null 0 Nov 13, 2021
A server which delivers client configuration settings to an end-user over http.

RuneTopic HTTP Server A server which delivers client configuration settings to an end-user over http. Setup Guide You can host a http server with Dock

Runetopic 2 Dec 1, 2021
Webclient-kotlin-sample - An example of using the http web client to promote synchronous and asynchronous https calls

Web Client Consumer Kotlin Sample The project is an example of using the http we

null 1 May 1, 2022
Multiplatform HTTP-Client implementation using Ktor

Kotlin Multiplatform HTTP-Client using Ktor Simple demonstration of a Ktor-based multiplatform HTTP-client in Kotlin Overview base submodule: common c

Jan Weidenhaupt 2 Oct 21, 2022
Tiny library to ease the use of environment variables with support for .env files

asimov/environment Tiny library to ease the use of environment variables with support for .env files. Installation Gradle (Kotlin) repositories {

Nicolas Bottarini 1 Jan 8, 2022
A springboot secure web app with thymeleaf support.

kotlin-web-maven-spring-thyme-challenge-question-aes-encoded-scrypt-encode Description A springboot secure web app with thymeleaf support. Three roles

null 0 Nov 23, 2021
A springboot secure web app with jsp support.

kotlin-web-maven-spring-jsp-register-rsa-encrypt-argon2-encoded Description A springboot secure web app with jsp support. Three roles are defined; USE

null 0 Nov 24, 2021