Skip to content

zeropercenthappy/RetrofitUtils

Repository files navigation

RetrofitUtils 简体中文

Download

Step 1.

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2.

Add the dependency

dependencies {
    // For android compat
    implementation 'com.github.zeropercenthappy:RetrofitUtils:1.2.1'
    // For androidX
    implementation 'com.github.zeropercenthappy:RetrofitUtils:1.3.5'
}

Usage

RetrofitBuilder

val retrofit = RetrofitBuilder()
    .baseUrl(url)
    // option: if you want to disable cookies manager (lib will handle cookies default, if you set it to false, remember to handle it yourself)
    .handleCookie(false)
    // option: if you have extra quest params to add
    .addParam(key, value)
    // option: if you have custom header to add
    .addHeader(key, value)
    // option: if you have other interceptor to add
    .addInterceptor(yourInterceptor)
    // option: if you have other network interceptor to add
    .addNetworkInterceptor(yourInterceptor)
    // option: if you want to define connect timeout (default is 10 sec)
    .connectTimeout(10_000)
    // option: if you want to define read timeout (default is 10 sec)
    .readTimeout(10_000)
    // option: if you want to define write timeout (default is 10 sec)
    .writeTimeout(10_000)
    // option: if you have converter to add, like GsonConverter or others
    .addConverterFactory(GsonConverterFactory.create())
    // option: if you have call adapter to add
    .addCallAdapterFactory(yourCallAdapter)
    // option: if you want to modify okHttp builder
    .okHttpClientBuilderOption { okHttpBuilder ->

    }
    // option: if you want to set max cache size (set it as -1 to disable)
    .setMaxCacheSize(1024_000)
    .build(context)
val api = retrofit.create(Api::class.java)

RequestBodyBuilder

Create text/plain body:

RequestBodyBuilder.createText(content)

Create multilpart body part: (It will handle file mimetype itself, just put a map with key and file)

RequestBodyBuilder.createMultipartBodyPartList(fileMap)

Create application/json body:

RequestBodyBuilder.createJson(json)

Other

StringConverter

Here offer a StringConverter for simple request that with response is String:

addConverterFactory(StringConverterFactory())

Then you can define a request with a String response:

@FormUrlEncoded
@POST(Url.POST)
fun query(@Field("name") name: String): Call<String>

CoroutineCallAdapter

Here offer a CoroutineConverter that support request in coroutine scope which just need several lines code:

addCallAdapterFactory(CoroutineCallAdapterFactory())

Then you can define a requst with CoroutineCall response:

@FormUrlEncoded
@POST(Url.POST)
fun query(@Field("name") name: String): CoroutineCall<QueryBean>

Start this request in a coroutine scope:

try {
    val coroutineCall = api.query(name)
    val queryBean = coroutineCall.request()
    // request success, do your other logic work
} catch (e: Exception) {
    // request fail or cancel
    e.printStackTrace()
}

BTW, since Retrofit 2.6.0, it support coroutine natural, you can define a request with suspend, and response is just the result :

@FormUrlEncoded
@POST(Url.POST)
suspend fun query(@Field("name") name: String): QueryBean

Then start this quest in a coroutine scope:

try {
    val queryBean = api.query(name)
    // request success, do your other logic work
} catch (e: Exception) {
    // request fail or cancel
    e.printStackTrace()
}

You can learn more by yourself if you interested :)

Log Interceptor

OkHttpInterceptor

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages