Android Easy Http - Simplest android http request library.

Overview

Android Easy Http Library

繁體中文文檔

About Android Easy Http Library

  • Made on OkHttp.
  • Easy to do http request, just make request and listen for the response.
  • Visual http request record, make you debug easily.
  • Http cookies persistence and editable.

Using Library In Your Application

Add this in your app build.gradle file.

android {
    buildFeatures {
        viewBinding true
        dataBinding true
    }
}

dependencies {
    implementation 'io.github.af19git5:easy-http-android:0.0.4'
}

Add permission in your AndroidManifest.xml

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

And check your application minSdk must be greater than 24.

Do Request

GET Request

Kotlin Example:

EasyHttp.get(context, url)
  .build()
  .getAsString(object : StringResponseListener {
    override fun onSuccess(headers: Headers, body: String) {
      // Do something...
    }

    override fun onError(e: HttpException) {
      // Do something...
    }
  })

Java Example:

EasyHttp.get(context, url)
  .build()
  .getAsString(new StringResponseListener() {
    @Override
    public void onSuccess(@NonNull Headers headers, @NonNull String body) {
      // Do something...
    }

    @Override
    public void onError(@NonNull HttpException e) {
      // Do something...
    }
  });

Value Settings:

  • .tag - Add tag for request, can use in cancel request.
  • .addUrlParams - Add url query param.
  • .urlParams - Add url query params.
  • .addHeader - Add header for request.
  • .headers - Add headers for request.
  • .cacheable - Cache response.
  • .saveRecord - Do save http request record. Default value is true.
  • .connectTimeout - Connect timeout time.
  • .readTimeout - Read timeout time.
  • .writeTimeout - Write timeout time.

Post Request

Kotlin Example:

EasyHttp.post(context, url)
  .jsonBody(obj)
  .build()
  .getAsString(object : StringResponseListener {
    override fun onSuccess(headers: Headers, body: String) {
      // Do something...
    }

    override fun onError(e: HttpException) {
      // Do something...
    }
  })

Java Example:

EasyHttp.post(context, url)
  .jsonBody(obj)
  .build()
  .getAsString(new StringResponseListener() {
    @Override
    public void onSuccess(@NonNull Headers headers, @NonNull String body) {
      // Do something...
    }

    @Override
    public void onError(@NonNull HttpException e) {
      // Do something...
    }
  })

Value Settings:

Have the value settings in the get example.

  • .stringBody - Set string request body, also can set your custom content type.
  • .jsonBody - Set json object request body, also can set your custom content type.
  • .formBody - Set form request body.
  • .requestBody - Set your custom okhttp request body.

Upload File

Kotlin Example:

EasyHttp.upload(context, url)
  .addMultipartFile("file", file)
  .addMultipartParameter("text", "text")
  .build()
  .getAsString(object : StringResponseListener {
    override fun onSuccess(headers: Headers, body: String) {
      // Do something...
    }

    override fun onError(e: HttpException) {
      // Do something...
    }
  })

Java Example:

EasyHttp.upload(context, url)
  .addMultipartFile("file", file)
  .addMultipartParameter("text", "text")
  .build()
  .getAsString(new StringResponseListener() {
    @Override
    public void onSuccess(@NonNull Headers headers, @NonNull String body) {
      // Do something...
    }

    @Override
    public void onError(@NonNull HttpException e) {
      // Do something...
    }
  })

Value Settings:

Have the value settings in the get example.

  • .contentType - Set upload request content type. Default value is "multipart/form-data".
  • .addMultipartParam - Add multipart parameter.
  • .addMultipartFile - Add multipart parameter. You can put File, ByteArray, Uri.

Cancel Request

Kotlin Example:

EasyHttp.cancel(tag)

Java Example:

EasyHttp.cancel(tag);

Custom OkHttpClient Builder

You can easily get OkHttpClient Builder and modify it.

Kotlin Example:

val easyHttpBuilder = EasyHttp.get(context, url)
val okHttpClientBuilder = easyHttpBuilder.okHttpClientBuilder

// Do something modify for OkHttpClient Builder...

easyHttpBuilder
  .build()
  .getAsString(object : StringResponseListener{
    override fun onSuccess(headers: Headers, body: String) {
      // Do something...
    }

    override fun onError(e: HttpException) {
      // Do something...
    }
  })

Java Example:

RequestBuilder requestBuilder = EasyHttp.get(context, url);
OkHttpClient.Builder okHttpClientBuilder = requestBuilder.getOkHttpClientBuilder();

// Do something modify for OkHttpClient Builder...

requestBuilder.build().getAsString(new StringResponseListener() {
  @Override
  public void onSuccess(@NonNull Headers headers, @NonNull String body) {
	// Do something...
  }

  @Override
  public void onError(@NonNull HttpException e) {
	// Do something...
  }
});

Response Listener

Get response as String

Kotlin Example:

EasyHttp.get(context, url)
  .build()
  .getAsString(object : StringResponseListener {
    override fun onSuccess(headers: Headers, body: String) {
      // Do something...
    }

    override fun onError(e: HttpException) {
      // Do something...
    }
  })

Java Example:

EasyHttp.get(context, url)
  .build()
  .getAsString(new StringResponseListener() {
    @Override
    public void onSuccess(@NonNull Headers headers, @NonNull String body) {
      // Do something...
    }

    @Override
    public void onError(@NonNull HttpException e) {
      // Do something...
    }
  });

Get json response as custom object

Kotlin Example:

// Use class
EasyHttp.get(context, url)
  .build()
  .getJsonAsObject(CustomObj::class.java, object : JsonResponseListener<CustomObj> {
    override fun onSuccess(headers: Headers, body: CustomObj)  {
      // Do something...
    }

    override fun onError(e: HttpException) {
      // Do something...
    }
  })

// Use Gson TypeToken
EasyHttp.get(context, url)
  .build()
  .getJsonAsObject(
    object : TypeToken<CustomObj>() {},
    object : JsonResponseListener<CustomObj> {
      override fun onSuccess(headers: Headers, body: CustomObj) {
        // Do something...
      }

      override fun onError(e: HttpException) {
        // Do something...
      }
    })

Java Example:

// Use class
EasyHttp.get(context, url)
  .build()
  .getJsonAsObject(
    CustomObj.class,
    new JsonResponseListener<>() {
      @Override
      public void onSuccess(@NonNull Headers headers, CustomObj body) {
        // Do something...
      }

      @Override
      public void onError(@NonNull HttpException e) {
        // Do something...
      }
  });

// Use Gson TypeToken
EasyHttp.get(context, url)
  .build()
  .getJsonAsObject(
    new TypeToken<String>() {},
    new JsonResponseListener<>() {
      @Override
      public void onSuccess(@NonNull Headers headers, CustomObj body) {
        // Do something...
      }

      @Override
      public void onError(@NonNull HttpException e) {
        // Do something...
      }
  });

Get response as bitmap

Kotlin Example:

EasyHttp.get(context, url)
  .build()
  .getAsBitmap(file, object : BitmapResponseListener {
    override fun onSuccess(headers: Headers, bitmap: Bitmap) {
      // Do something...
    }

    override fun onProgress(downloadBytes: Long, totalBytes: Long) {
      // Do something...
    }

    override fun onError(e: HttpException) {
      // Do something...
    }
  })

Java Example:

EasyHttp.get(context, url)
  .build()
  .getAsBitmap(file, new BitmapResponseListener() {
    @Override
    public void onSuccess(@NonNull Headers headers, @NonNull Bitmap bitmap) {
       // Do something...
    }

    @Override
    public void onProgress(long downloadBytes, long totalBytes) {
      // Do something...
    }

    @Override
    public void onError(@NonNull HttpException e) {
      // Do something...
    }
  });

Download response

Kotlin Example:

EasyHttp.get(context, url)
  .build()
  .download(file, object : DownloadListener {
    override fun onSuccess(headers: Headers, file: File) {
      // Do something...
    }

    override fun onProgress(downloadBytes: Long, totalBytes: Long) {
      // Do something...
    }

    override fun onError(e: HttpException) {
      // Do something...
    }
  })

Java Example:

EasyHttp.get(context, url)
  .build()
  .download(file, new DownloadListener() {
    @Override
    public void onSuccess(@NonNull Headers headers, @NonNull File file) {
       // Do something...
    }

    @Override
    public void onProgress(long downloadBytes, long totalBytes) {
      // Do something...
    }

    @Override
    public void onError(@NonNull HttpException e) {
      // Do something...
    }
  });

Cookie Modify

Clear cookies

Kotlin Example:

// clear all
EasyHttp.clearCookies(context)
// clear by host
EasyHttp.clearCookies(context, host)
// clear by host and cookie name
EasyHttp.clearCookies(context, host, name)

Java Example:

// clear all
EasyHttp.clearCookies(context);
// clear by host
EasyHttp.clearCookies(context, host);
// clear by host and cookie name
EasyHttp.clearCookies(context, host, name);

Android EasyHttp Page

Http Record Page

This page can show Easy Http record.

If your request saveRecord is open, you can find record in the http record page.

Preview:

You can use this code to intent record page.

Kotlin Example:

EasyHttp.intentEasyHttpRecord(context)

Java Example:

EasyHttp.intentEasyHttpRecord(context);

Cookies Page

This page can show Easy Http persistence cookies.

Also you can modify it.

Preview:

You can use this code to intent cookies page.

Kotlin Example:

EasyHttp.intentEasyHttpCookies(context)

Java Example:

EasyHttp.intentEasyHttpCookies(context);

Demo App

License

Copyright 2022 Jimmy Kang

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.
You might also like...
Asynchronous Http and WebSocket Client library for Java

Async Http Client Follow @AsyncHttpClient on Twitter. The AsyncHttpClient (AHC) library allows Java applications to easily execute HTTP requests and a

HttpMocker is a simple HTTP mocking library written in Kotlin to quickly and easily handle offline modes in your apps

HttpMocker HttpMocker is a very lightweight Kotlin library that allows to mock HTTP calls relying on either OkHttp or the Ktor client libraries. It ca

Kotlin HTTP requests library. Similar to Python requests.

khttp khttp is a simple library for HTTP requests in Kotlin. It functions similarly to Python's requests module. import khttp.get fun main(args: Arra

Unirest in Java: Simplified, lightweight HTTP client library.

Unirest for Java Install With Maven: !-- Pull in as a traditional dependency -- dependency groupIdcom.konghq/groupId artifactIdunire

super simple library to manage http requests.

HttpAgent super simple library to manage http requests. Gradle dependencies { implementation 'com.studioidan.httpagent:httpagent:1.0.16@aar' } No

Unirest in Java: Simplified, lightweight HTTP client library.

Unirest for Java Install With Maven: !-- Pull in as a traditional dependency -- dependency groupIdcom.konghq/groupId artifactIdunire

Square’s meticulous HTTP client for the JVM, Android, and GraalVM.

OkHttp See the project website for documentation and APIs. HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP

A type-safe HTTP client for Android and the JVM

Retrofit A type-safe HTTP client for Android and Java. For more information please see the website. Download Download the latest JAR or grab from Mave

HTTP Server for Android Instrumentation tests

RESTMock REST API mocking made easy. RESTMock is a library working on top of Square's okhttp/MockWebServer. It allows you to specify Hamcrest matchers

Releases(v0.0.6)
Owner
null
Java HTTP Request Library

Http Request A simple convenience library for using a HttpURLConnection to make requests and access the response. This library is available under the

Kevin Sawicki 3.3k Jan 6, 2023
MVVM with simple HTTP Request Example

Minimum MVVM Koin (Kotlin Dependency Injection) Coroutines View Model Lifecycle News API Retrofit Rx Java Rx Android OkHttp Client Result ScreenShot 1

Faisal Amir 6 Dec 1, 2022
Allows recording and playback http request/responses for testing purposes.

Allows recording and playback http request/responses for testing purposes.

Cristian Gómez 4 Aug 4, 2022
Allows recording and playback http request/responses for testing purposes.

Test Record Interceptor est Record Interceptor allows to record responses from http request to be replayed at tests. How it works Use regular http cal

Cristian Gómez 4 Aug 4, 2022
This application provide purchase request service for Membership Console.

Purchase Request 概要 本プロジェクトはMembership Consoleの購入申請機能を提供します。 開発 開発環境 Java OpenJDK 17 Kotlin 1.7.10 Ktor 2.1.2 PostgreSQL 13 docker-compose ビルド方法 ビルドに成

Membership Console 2 Nov 17, 2022
🚀 A Complete Fast Android Networking Library that also supports HTTP/2 🚀

Fast Android Networking Library About Fast Android Networking Library Fast Android Networking Library is a powerful library for doing any type of netw

AMIT SHEKHAR 5.5k Dec 27, 2022
The easiest HTTP networking library for Kotlin/Android

Fuel The easiest HTTP networking library for Kotlin/Android. You are looking at the documentation for 2.x.y.. If you are looking for the documentation

Kittinun Vantasin 4.3k Jan 8, 2023
Asynchronous socket, http(s) (client+server) and websocket library for android. Based on nio, not threads.

AndroidAsync AndroidAsync is a low level network protocol library. If you are looking for an easy to use, higher level, Android aware, http request li

Koushik Dutta 7.3k Jan 2, 2023
🚀 A Complete Fast Android Networking Library that also supports HTTP/2 🚀

Fast Android Networking Library About Fast Android Networking Library Fast Android Networking Library is a powerful library for doing any type of netw

AMIT SHEKHAR 5.5k Jan 3, 2023
Volley is an HTTP library that makes networking for Android apps easier and, most importantly, faster.

Volley Volley is an HTTP library that makes networking for Android apps easier and, most importantly, faster. For more information about Volley and ho

Google 3.3k Jan 1, 2023