webi

Additional

Language
Java
Version
4.1.1 (Feb 16, 2018)
Created
Dec 26, 2017
Updated
Aug 15, 2018 (Retired)
Owner
Alireza Ashrafi (alirezaashrafi)
Contributor
Alireza Ashrafi (alirezaashrafi)
1
Activity
Badge
Generate
Download
Source code

Do not forget the star:)⭐️

Webi is a Fast and full of features HTTP library that makes easy networking and caching response for Android apps And comparable to other similar libraries

  • Easy to use and set up
  • Less coding
  • Very fast and multi-threaded
  • Includes quick and different ways to cache
  • JsonArray , jsonObject and xml request
  • Easily post values to the server

راهنما فارسی اسفاده از کتابخانه


How to download webi

Gradle

Add it in your root build.gradle at the end of repositories:
    allprojects {
         repositories {
             ...
             maven { url 'https://jitpack.io' }
         }
    }
add this line to your module build.gradle dependecies block:
    compile 'com.github.alirezaashrafi:webi:4.1.0'

Maven

Add the JitPack repository to your build file
  <repositories>
    <repository>
        <id>jitpack.io</id>
      <url>https://jitpack.io</url>
    </repository>
  </repositories>
Add the dependency
  <dependency>
    <groupId>com.github.alirezaashrafi</groupId>
    <artifactId>webi</artifactId>
    <version>4.1.0</version>
  </dependency>

How to use webi

  Webi.with(context)
      .from("http://www.example.com")
      .onResponse(new OnResponse() {
          @Override
          public void Response(String response, String where) {
              //response is content of url
              //where is type of load response from e.g. online ram xml or sql
          }
      }).connect();

Retry on failed
  .setRetryTimes(int times) //Default is one time

  .setOnRetry(new OnRetry() {
      @Override
      public void retry(int remainingRetrys) {
          //call back when retry
      }
  });

Post with webi

.addPost(key,value,description); note: description is meta data and these are not sent with your HTTP request

  Webi.with(context)
      .from("http://www.example.com")
      .addPost("username","webi")
      .addPost("password","123456")
      .connect();
and receive this posts in php
  <?php
      $username = $_POST['username'];  //value webi
      $password = $_POST['password'];  //value 123456
  ?>
Post your custom list
  List<Posts>postsList = new ArrayList<>();
  Posts posts = new Posts();
  posts.setKey("...");
  posts.setValue("...");
  postsList.add(posts);
  ....

  Webi.with(context)
    .from("http://www.example.com")
    .addPostList(postsList)
    .connect();

Add GET method params
  Webi.with(context)
      .from("http://www.example.com")
      .addGet("username","webi")
      .addGet("password","123456")
      .connect();

      //res http://www.example.com?username=webi&password=123456
and get that values in php
  <?php
      $username = $_GET['username'];  //value webi
      $password = $_GET['password'];  //value 123456
  ?>

Add Headers
  Webi.with(context)
      .from("http://www.example.com")
      .addHeader("Content-Type","application/json")
      .addHeader("Authorization","Bearer.....")
      .connect();
If you want to send bearer token in header use this method
  .setBearerToken("e.g. GJKRY78579tFYIlkdhiipEE908y0FD80")

JsonArray , JsonObject and XML request with webi
  Webi.with(this).from("http://www.example.com")
      .setOnJsonArrayReceive(new OnJsonArrayReceive() {
          @Override
          public void jsonArray(JSONArray jsonArray, String where) {
              //return json array if content is a jsonArray
              //note jsonArray First character is '[' and the last is ']'

          }
      })
      .setOnJsonObjectReceive(new OnJsonObjectReceive() {
          @Override
          public void jsonObject(JSONObject jsonObject, String where) {
              //return json object if content is a json object
              //note JsonObject First character is '{' and the last is '}'

          }
      })
      .setOnXmlRecevie(new OnXmlReceive() {
          @Override
          public void xml(Document xml, String where) {
              //return xml if content is xml

          }
      }).connect();

Caching in webi
There are three ways to cache
  • ram cache - fastest but temporary
  • xml cache - fast but cache only 50 response
  • sql cache - permanent and Unlimited cache about 5,000 response but Slower than the rest
  Webi.with(context)
      .from("http://www.example.com")
      .setSqlCache(true) //It is better to have only one active Caching method
      .setXmlCache(true)
      .setRamCache(true)
      .connect();
.setSqlCache(true,key)

.setXmlCache(true,key)

Your custom key for storage If you do not set, an automatic hash (MD5) key will be generated

Work offline

When work offline is enable, content is only read from cache and cache content is not update

  .setWordOffline(true)

Encrypt cache content
When encryptCache is enabled, the data is encrypt before insertion
  .setEncryptCache(true)

Set custom connectTimeOut & readTimeOut
  .setConnectTimeOut(10000)
  .setReadTimeOut(10000)
  //The default of both is 5000 ms

Get Response Time
  Webi.with(context)
    .from("http://www.example.com")
    .getResponseTime(new GetResponseTime() {
        @Override
        public void time(long time) {
            //e.g. 430 ms
        }
    }).

call back when request failed
  .setOnFailed(new OnFailed() {
      @Override
      public void failed(int code) {
          //code is http response code e.g. 404
      }
  }).

use proxy in Webi
.setProxy(host,port).
   or
.setProxy(host,port,username,password).

WebiConfig.init()

Use this class to change web defaults

    WebiConfig.init()
        .setDefaultUrl("http://alirezaashrafi.ir")
        .setDefaultConnectTimeOut(15000)
        .setDefaultReadTimeOut(15000);

    Webi.with(this).onResponse(new OnResponse() {
        @Override
        public void Response(String res, String where) {

        }
    });
It will better performance if call and set first of all in application class
  public class myApplication extends Application {
      @Override
      public void onCreate() {
          WebiConfig.init()
                  .setDefaultUrl("http://alirezaashrafi.ir")
                  .setDefaultConnectTimeOut(15000)
                  .setDefaultReadTimeOut(15000);


          super.onCreate();
      }
  }
manifest
<manifest>
    <application
        android:name=".myApplication"
        ...
        >

    </application>
</manifest>
some of WebiConfig class methods


Licence

Copyright 2017 Alireza Ashrafi

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.


Author


If you liked this library, do not forget to star and follow me ⭐️❤️️????

Eventually see my other libraries and projects

. . . .