那麼今天主要要用RxJava來結合retrofit做Post的部分,與上次用Retrofit的call類似,但這次是要用Observe(被觀察者)來做,json假資料:posts,那麼首先先加入依賴。
老樣子,在gradle:app的dependcies加入依賴及retrofit需要用到的網路權限,今天要加入的比較多。
implementation 'io.reactivex.rxjava2:rxjava:2.0.1'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'
將網路的權限加入至AndroidManifest.xml中。
<uses-permission android:name="android.permission.INTERNET" />
接著我們就開始進行設計,首先一樣宣告posts的Model,定義網站的四個data(userId、Id、Title、Body)然後+getter、setter,一樣可由右鍵->generate->gsonformatplus新增(需先安裝plugins):
public class Model {
private Integer userId;
private Integer id;
private String title;
private String body;
public Model(Integer userId , Integer id , String title , String body){
this.userId=userId;
this.id=id;
this.title=title;
this.body=body;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
Model這邊定義完資料後,那麼接著就看到接口Interface。
//@POST("posts")
//Call<Model> getCall(@Body Model data);
@POST("posts")
Observable<Response<Model>> getCall(@Body Model data);//宣告成Observable<Response<?>>,接收Response物件(回傳的東西)
比較不一樣的是之前是用Call來進行呼叫,而這次是要宣告成Observable(被觀察者)然後從主程式使用Observer(觀察者)將資料讀出。
public class Manager {
private static Manager mInstance = new Manager();
private MyApiService myApiService;
private Manager() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())//新增此行,rxjava創建Observable對象的調配器(紅字的話要檢查gradle有沒有安裝rxjava2-adapter的依賴)
.build();
myApiService = retrofit.create(MyApiService.class);
}
public static Manager getInstance() {
return mInstance;
}
public MyApiService getAPI() {
return myApiService;
}
}
接著可能會簡單的帶到subscribeOn與observeOn這些線程操作。
public class MainActivity extends AppCompatActivity {
Observable<Response<Model>> model;//被觀察者object
MyApiService myApiService;//api interface object
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Model data=new Model(10,20,"This is a title","This is a body");//新增的資料
myApiService=Manager.getInstance().getAPI();
model = myApiService.getCall(data);//使用getCall方法
model.subscribeOn(Schedulers.io())//I/O模式,可指定Observer在特定的調度上觀察Observable
.observeOn(AndroidSchedulers.mainThread())//指定Observable在特定的調度上進行動作
.subscribe(new Observer<Response<Model>>() {//訂閱建立觀察者
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Response<Model> value) {
Log.d("Post",""+value.code());
Log.d("Post","userid:"+value.body().getUserId());
Log.d("Post","id:"+value.body().getId());
Log.d("Post","title:"+value.body().getTitle());
Log.d("Post","body:"+value.body().getBody());
}
@Override
public void onError(Throwable e) {
Log.d("Post","Error");
}
@Override
public void onComplete() {
}
});
}
}