Code Monkey home page Code Monkey logo

webi's Introduction

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 ⭐️❤️️💙

. . . .

webi's People

Contributors

alirezaashrafi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

webi's Issues

Webi duplicating requests

The problem is two-fold. Webi sending duplicate requests behind the scene, and duplicated post data.

  1. Duplicated requests - Code as follows:

Webi.with(this)
.from(someURL)
.addPost("user", user)
.addPost("code", code)
.addPost("values", values)
.onResponse(new OnResponse() {
@OverRide
public void Response(String response, String where) {}
})
.setOnFailed(new OnFailed() {
@OverRide
public void failed(int code) {}
})
.setRetryTimes(0)
.connect();

LogCat shows that this function is called once, and success also reported once, but on server side, I see duplicated entries:

x.x.x.x - - [08/Mar/2018:07:30:15 +0000] "POST /xxx.php HTTP/1.1" 200 379 "-" "Dalvik/2.1.0 (Linux; U; Android 6.0; Redmi Note 4 MIUI/V9.2.2.0.MBFMIEK)"
x.x.x.x - - [08/Mar/2018:07:30:15 +0000] "POST /xxx.php HTTP/1.1" 200 365 "-" "Dalvik/2.1.0 (Linux; U; Android 6.0; Redmi Note 4 MIUI/V9.2.2.0.MBFMIEK)"

Notice the requests are sent basically at the same time, with a slightly different payload.

  1. Post data duplication.

Logcat as follows:

com.ashrafi.webi.classes.PostDataBuilder: buildPostData: user=AAAA&code=BBBB&user=AAAA&code=BBBB&values=%5B%5B1520493226272%2C0%2C3.519%2C22.2803964%2C114.188337%2C0%2C0.0%5D%2C%5B1520493234846%2C0%2C0.086%2C22.2803964%2C114.188337%2C0%2C0.0%5D%5D

You can see in this example that user and code are duplicated. In other samples I also see "values" re-appearing (I did not check if the data is the same - too long).

Parameters becoming array

Good work done. The only issue i am facing is parameters become array when request are made twice or more.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.