我如何使用 RxJava2 和 Retrofit2 执行 POST 请求?
How I can do a POST request with RxJava2 and Retrofit2??
我正在尝试一起学习 RxJava2 和 Retrofit,现在,我知道如何调用来发出 GET 请求。但是我不知道怎么做 POST, PUT... 等等
我的 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 请求?谢谢。
首先,我鼓励您使用 retrolambda plugin 以减少 RxJava 方法的冗长。
对于请求,要发出 POST 请求,您必须发送表示请求正文的对象。我认为您要实现的目标是:
@POST(LiveDataApi.POST_LOGIN)
Flowable<HttpCustomRes<User>> loginUser(@Body UserBody user);
还有 UserBody 对象,我想应该是这样的:
class UserBody {
private final String user;
private final String password;
UserBody(String user, String password) {
this.user = user;
this.password = password;
}
}
我建议您阅读 Retrofit docs,其中更好地解释了如何提出正确的请求。
我正在尝试一起学习 RxJava2 和 Retrofit,现在,我知道如何调用来发出 GET 请求。但是我不知道怎么做 POST, PUT... 等等
我的 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 请求?谢谢。
首先,我鼓励您使用 retrolambda plugin 以减少 RxJava 方法的冗长。
对于请求,要发出 POST 请求,您必须发送表示请求正文的对象。我认为您要实现的目标是:
@POST(LiveDataApi.POST_LOGIN)
Flowable<HttpCustomRes<User>> loginUser(@Body UserBody user);
还有 UserBody 对象,我想应该是这样的:
class UserBody {
private final String user;
private final String password;
UserBody(String user, String password) {
this.user = user;
this.password = password;
}
}
我建议您阅读 Retrofit docs,其中更好地解释了如何提出正确的请求。