shichaohui / RetrofitWrapper

Packing Retrofit and makes it easier to use.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RetrofitWrapper

Packing Retrofit and makes it easier to use.

Feature

  • Support url params.
  • Support form params.
  • Support raw params.
  • Support multipart params.
  • Support custom headers.

Usage

Java Example and Kotlin Example

dependencies

implementation 'com.sch.retrofit:retrofit-wrapper:1.0.0'

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'

implementation 'com.google.code.gson:gson:2.8.5'

implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'io.reactivex.rxjava2:rxjava:2.1.16'

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

Init

RetrofitWrapper.init("http://www.tngou.net/")

RetrofitWrapper.init("http://www.tngou.net/", Level.BODY)

RetrofitWrapper.init(retrofit)

Create request body by extend inner params

class Request(params: Int) : UrlParams<Response>("api") {

    /*
     * url:baseUrl/api?params=params
     */

    // Response type for Gson convert.
    override val responseType: Type = Response::class.java

    init {
        putUrlParams("params", params)
    }

}
class Request(urlParams: Int, formParams: Int) : FormParams<Response>("api") {

    /*
     * url:baseUrl/api?params=params
     * form params: params=params
     */

    // Response type for Gson convert.
    override val responseType: Type = Response::class.java

    init {

        putUrlParams("params", urlParams)

        putFormParams("params", formParams)
    }

}
class Request(urlParams: Int, rawParams: Int) : RawParams<Response>("api") {

    /*
     * url:baseUrl/api?params=params
     * raw params: {"params":"params"}
     */

    // Response type for Gson convert.
    override val responseType: Type = Response::class.java

    init {

        putUrlParams("params", urlParams)

        putRawParams("params", rawParams)
    }

}

More usage about inner params.

Create request body by custom params

Use interface ( SupportUrlParams SupportFormParams SupportRawParams SupportMultipartParams SupportHeaders ) just like inner params .

Initiate a request

// Method: GET
ApiRequester.get(Request(params))
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe { response ->
            // do something
        }

// Method: POST, Params: Form, Raw, Multipart
ApiRequester.post(Request(params))
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe { response ->
            // do something
        }

More usage about initiate a request.

License

Copyright (c) 2015-2018 Shi ChaoHui

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.

About

Packing Retrofit and makes it easier to use.


Languages

Language:Kotlin 85.3%Language:Java 14.7%