依赖
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.8'implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
Retrofit版本2.5.0
RxJava版本2.2.8
converter-gson:用于将来自服务端的json数据转换为Java对象
adapter-rxjava:用于实现Retrofit和RxJava的组合
rxandroid:用于使用AndroidSchedulers.mainThread()
示例
EyeKeyService.java
package com.bu_ish.retrofitrxjava;
import io.reactivex.Single;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
public interface EyeKeyService {
String BASE_URL = "http://api.eyekey.com/face/";
String APP_ID = "f89ae61fd63d4a63842277e9144a6bd2";
String APP_KEY = "af1cd33549c54b27ae24aeb041865da2";
@FormUrlEncoded
@POST("Check/checking")
Single<FaceCheckingReply> checkFace(@Field("app_id") String appId, @Field("app_key") String appKey, @Field("url") String url);
}
MainActivity.java
package com.bu_ish.retrofitrxjava;
import android.app.ProgressDialog;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
private ProgressDialog pdHttpRequest;
private EyeKeyService service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdHttpRequest = new ProgressDialog(this);
pdHttpRequest.setCancelable(false);
findViewById(R.id.btnCheckFace).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String picUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1554372634204&di=a4804907c80d38d4997291e0cb546f71&imgtype=0&src=http%3A%2F%2Fi1.sinaimg.cn%2Fent%2F2011%2F1012%2FU3593P28DT20111012165707.jpg";
service.checkFace(EyeKeyService.APP_ID, EyeKeyService.APP_KEY, picUrl)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<FaceCheckingReply>() {
@Override
public void onSubscribe(Disposable d) {
pdHttpRequest.show();
}
@Override
public void onSuccess(FaceCheckingReply faceCheckingReply) {
pdHttpRequest.dismiss();
String code = faceCheckingReply.getResCode();
if (code.equals("0000")) {
String faceId = faceCheckingReply.getFaces().get(0).getFaceId();
new AlertDialog.Builder(MainActivity.this).setTitle("Face Id").setMessage(faceId).show();
} else {
new AlertDialog.Builder(MainActivity.this).setTitle(code).setMessage(faceCheckingReply.getMessage()).show();
}
}
@Override
public void onError(Throwable e) {
pdHttpRequest.dismiss();
new AlertDialog.Builder(MainActivity.this).setTitle("Error").setMessage(e + "").show();
}
});
}
});
service = new Retrofit.Builder()
.baseUrl(EyeKeyService.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(EyeKeyService.class);
}
}
其中的addConverterFactory(GsonConverterFactory.create())用于实现json转换为Java对象
若不添,则抛出如下异常
Caused by: java.lang.IllegalArgumentException: Could not locate ResponseBody converter for class com.bu_ish.retrofitrxjava.FaceCheckingReply.
addCallAdapterFactory(RxJava2CallAdapterFactory.create())用于实现将EyeKeyService中的函数类型设定为RxJava中的被观察者类型
若不添,则抛出如下异常
Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for io.reactivex.Single<com.bu_ish.retrofitrxjava.FaceCheckingReply>.
subscribeOn(Schedulers.io())用于把网络请求切换至子线程
若不添,则抛出异常NetworkOnMainThreadException
observeOn(AndroidSchedulers.mainThread())用于指定在主线程中处理服务端的响应数据