NetworkConnectionLibrary

Library used in the simple http request

Лицензия

Лицензия

Категории

Категории

Сеть
Группа

Группа

com.github.rudda
Идентификатор

Идентификатор

networkconnetion
Последняя версия

Последняя версия

0.0.1
Дата

Дата

Тип

Тип

aar
Описание

Описание

NetworkConnectionLibrary
Library used in the simple http request
Ссылка на сайт

Ссылка на сайт

https://github.com/rudda/NetworkConnection
Система контроля версий

Система контроля версий

https://github.com/rudda/NetworkConnection

Скачать networkconnetion

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/com.github.rudda/networkconnetion/ -->
<dependency>
    <groupId>com.github.rudda</groupId>
    <artifactId>networkconnetion</artifactId>
    <version>0.0.1</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.github.rudda/networkconnetion/
implementation 'com.github.rudda:networkconnetion:0.0.1'
// https://jarcasting.com/artifacts/com.github.rudda/networkconnetion/
implementation ("com.github.rudda:networkconnetion:0.0.1")
'com.github.rudda:networkconnetion:aar:0.0.1'
<dependency org="com.github.rudda" name="networkconnetion" rev="0.0.1">
  <artifact name="networkconnetion" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.github.rudda', module='networkconnetion', version='0.0.1')
)
libraryDependencies += "com.github.rudda" % "networkconnetion" % "0.0.1"
[com.github.rudda/networkconnetion "0.0.1"]

Зависимости

compile (3)

Идентификатор библиотеки Тип Версия
com.android.support » appcompat-v7 jar 25.3.1
com.google.code.gson : gson jar 2.2.4
com.mcxiaoke.volley : library-aar jar 1.0.1

Модули Проекта

Данный проект не имеет модулей.

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 <string, string> 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

  
  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/

Версии библиотеки

Версия
0.0.1