Coder Social home page Coder Social logo

retrofitflowcalladapter's Introduction

RetrofitFlowCallAdapter

Retrofit + Flow 最佳实践

  • 支持异步、同步

  • 支持body、response

使用

添加 FlowCallAdapterFactory 异步方式

val retrofit = Retrofit.Builder()
        .baseUrl("https://api.github.com")
        .addCallAdapterFactory(FlowCallAdapterFactory.createAsync())
        .addConverterFactory(GsonConverterFactory.create())
        .build()

添加 FlowCallAdapterFactory 同步方式

val retrofit = Retrofit.Builder()
        .baseUrl("https://api.github.com")
        .addCallAdapterFactory(FlowCallAdapterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .build()

定义接口

interface ApiService {

    @GET("users/{user}/repos")
    fun listReposWithBody(@Path("user") user: String): Flow<List<Repo>>

    @GET("users/{user}/repos")
    fun listReposWithResponse(@Path("user") user: String): Flow<Response<List<Repo>>>
}

发起请求

lifecycleScope.launch {
     service.listReposWithResponse("zyj1609wz")
            .flowOn(Dispatchers.IO)
             .catch {
                 it.printStackTrace()
                 Log.d("my--", "error:${it.message} ${Thread.currentThread().name}")
             }
             .collect {
                 val list = it.body()
                 val item = list?.get(0)
                 item?.let {
                     Log.d("my--", "onResponse:id:${item.id} name:${item.name}")
               }
           }
        }

Downlaod

Step 1. Add the JitPack repository to your build file:

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

Step 2. Add the dependency:

implementation 'com.github.zyj1609wz:RetrofitFlowCallAdapter:1.1.0'

retrofitflowcalladapter's People

Contributors

zyj1609wz avatar

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.