1
我正試圖學習RxJava2和Retrofit,現在我知道如何調用GET請求。但我不知道該怎麼做,POST,PUT ...等如何使用RxJava2和Retrofit2做POST請求?
我AccountUserApi文件是:
@GET(LiveDataApi.GET_USER)
Flowable<HttpCustomRes<UserPostData>> getUserData(@Path("id") long userId);
...
@POST(LiveDataApi.POST_LOGIN)
Flowable<HttpCustomRes<User>> loginUser(@Body @Field("user") String username, @Body @Field("password") String password);
對於經理,我有:
public class AccountUserManager {
@Inject
public AccountUserManager(){}
public AccountUserApi getApi() {
return HttpRetrofit.getInstance().getService(AccountUserApi.class);
}
// THIS IS BAD :(
public Flowable<User> loginUser(String username, String password){
return getApi().loginUser(username, password).map(new Function<HttpCustomRes<User>, User>() {
@Override
public User apply(@NonNull HttpCustomRes<User> userPostDataHttpCustomRes) throws Exception {
if(userPostDataHttpCustomRes != null) {
return userPostDataHttpCustomRes.getData();
}else
return null;
}
});
}
public Flowable<UserPostData> getUserData(long userId){
return getApi().getUserData(userId).map(new Function<HttpCustomRes<UserPostData>, UserPostData>() {
@Override
public UserPostData apply(@NonNull HttpCustomRes<UserPostData> userPostDataHttpCustomRes) throws Exception {
if(userPostDataHttpCustomRes != null) {
return userPostDataHttpCustomRes.getData();
}else
return null;
}
});
}
public Flowable<EmptyModel> setUserData(long userId, UserPostData userPostData){
return getApi().setUserData(userId, userPostData).map(new Function<HttpCustomRes<EmptyModel>, EmptyModel>() {
@Override
public EmptyModel apply(@NonNull HttpCustomRes<EmptyModel> emptyModelHttpCustomRes) throws Exception {
if(emptyModelHttpCustomRes != null) {
return emptyModelHttpCustomRes.getData();
}else
return null;
}
});
}
}
我怎樣才能用RxJava2和Retrofit2做POST請求?謝謝。