我正在通过REST API注册用户。我正在使用RXJAVA和MVVM体系结构模式进行此操作。 User对象包装在称为RegisterResource类的包装器类的周围。它显示如下错误: java.lang.IllegalStateException:应为BEGIN_OBJECT,但位于第1行第2列路径$
regUser.setValue(RegisterResource.loading((User)null));
final LiveData<RegisterResource<User>> source = LiveDataReactiveStreams.
fromPublisher(registerApi.
registerUser(email, name, password)
.onErrorReturn(new Function<Throwable, User>() {
@Override
public User apply(Throwable throwable) throws Exception {
User errorUser = new User();
Log.d(TAG, "apply: "+ throwable.getMessage());
errorUser.setEmail("-1");
Log.d(TAG, "apply: Error " + errorUser.getEmail());
return errorUser;
}
})
.map(new Function<User, RegisterResource<User>>() {
@Override
public RegisterResource<User> apply(User user) throws Exception {
if (user.getEmail().equals("-1")) {
return RegisterResource.error("Could not Register", (User) null);
}
Log.d(TAG, "apply: Map Function " + user.getEmail());
return RegisterResource.authenticated(user);
}
}).subscribeOn(Schedulers.io()));
regUser.addSource(source, new Observer<RegisterResource<User>>() {
@Override
public void onChanged(RegisterResource<User> userRegisterResource) {
regUser.setValue(userRegisterResource);
regUser.removeSource(source);
}
});
REST API的结构为:
{"id":0,"unique_id":2,"name":"Sam 000","email":"sam@gmail.com","encrypted_password":"282492b281b968bc33b361ed8b2d3ad1e2fe861e0ec85d1c7549c626bcddc42428asdfasdfasfsfasfasfsfasfas135be30ffswjioiuias47911e8502ec1b830b21af7a4a6f24d3f733asdasjyowah00dbb4409e890c92cb","salt":"9fc29dljshdojhaspkdpqb25bda4sarfwdgAe8b","created_at":"2019-05-24T08:58:18.000Z","updated_at":"2019-05-24T08:58:18.000Z"}
活动如下
private void subscribeObservers(){
registerViewModel.observeUser().observe(this, new Observer<RegisterResource<User>>() {
@Override
public void onChanged(RegisterResource<User> userRegisterResource) {
if(userRegisterResource !=null){
switch (userRegisterResource.status) {
case LOADING:{
showProgressBar(true);
break;}
case AUTHENTICATED: {
showProgressBar(false);
Log.d(TAG, "onChanged: Registration SUCCESS : " + userRegisterResource.data.getEmail());
onRegistrationSuccess();
Toast.makeText(RegisterActivity.this, "Success", Toast.LENGTH_SHORT).show();
break;
}
case ERROR: {
showProgressBar(false);
Log.d(TAG, "onChanged: Error: " + userRegisterResource.message);
Toast.makeText(RegisterActivity.this, userRegisterResource.message , Toast.LENGTH_SHORT).
show();
break;
}
case NOT_AUTHENTICATED: {
showProgressBar(false);
break;
}
}
}
}
});
}