Retrofit2+RxJava3使用

开发工具:Androidstudio

Retrofit2的使用

1.先添加依赖

implementation 'com.squareup.retrofit2:retrofit:2.9.0'

2.实现请求方法

public interface RetrofitService {

    @GET("users/{user}/repos")
    Call listRepos(@Path("user") String user);

}

 3.在Activity里使用

class BaseRetrofitActivity: Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val retrofit = Retrofit.Builder()
            .baseUrl("https://api.github.com/")
            .build()

        val service = retrofit.create(RetrofitService::class.java)
        service.listRepos("octocat").enqueue(object : Callback {
            override fun onResponse(call: Call, response: Response) {
                val str = response.body()?.bytes()?.let { String(it) }
                System.currentTimeMillis()
            }

            override fun onFailure(call: Call, t: Throwable) {
                System.currentTimeMillis()
            }
        })
    }

}

Retrofit2+Converter

1.添加依赖

    implementation 'com.squareup.retrofit2:retrofit:2.9.0'

    //转换器
    implementation "com.google.code.gson:gson:2.8.6"
    implementation "com.squareup.retrofit2:converter-gson:2.9.0"

 2.实现请求方法

public interface AddConverterRetrofitService {

    @GET("users/{user}/repos")
    Call> listRepos(@Path("user") String user);

}

3.在Activity里使用

class AddConverterRetrofitActivity: Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val retrofit = Retrofit.Builder()
            .baseUrl("https://api.github.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        val service = retrofit.create(AddConverterRetrofitService::class.java)
        service.listRepos("octocat").enqueue(object : Callback> {
            override fun onResponse(call: Call>, response: Response>) {
                System.currentTimeMillis()
            }

            override fun onFailure(call: Call>, t: Throwable) {
                System.currentTimeMillis()
            }
        })
    }

}

Retrofit2+Converter+RxJava3

1.添加依赖

    implementation 'com.squareup.retrofit2:retrofit:2.9.0'

    //添加转换器
    implementation "com.google.code.gson:gson:2.8.6"
    implementation "com.squareup.retrofit2:converter-gson:2.9.0"

    //rxjava3
    implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'
    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'

 2.实现请求方法

public interface RxJavaAndAddConverterRetrofitService {

    @GET("users/{user}/repos")
    Observable> listRepos(@Path("user") String user);

}

3.在Activity里使用

class RxJavaAndAddConverterRetrofitActivity: Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val retrofit = Retrofit.Builder()
            .baseUrl("https://api.github.com/")
            .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        val service = retrofit.create(RxJavaAndAddConverterRetrofitService::class.java)
        service.
            listRepos("octocat")
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer>{
                override fun onSubscribe(d: Disposable) {
                    System.currentTimeMillis()
                }

                override fun onNext(t: List) {
                    System.currentTimeMillis()
                }

                override fun onError(e: Throwable) {
                    System.currentTimeMillis()
                }

                override fun onComplete() {
                    System.currentTimeMillis()
                }
            })
    }

}

你可能感兴趣的:(android,java,kotlin)