I have an Android app on Java that use the Volley library. I’m taking over the work of a former intern who make a RequestUtils class to create his own requests. I need to upgrade his work in order to make a more reliable app and I read a lot of documentation to know the best practices.
I need to get the user informations from my server to display it in my Activity, and if there is an error make a Toast. I use ViewModel to avoid HTTP request at each rotation change or whatever, and currently it’s the best for me if there is a request in the case of the Activity being entirely recreate because of the phone killing it in the background.
I’m at the point that I have a UserViewModel class and UserRepository class, and in this Repository class I want to use the request that he made for that, except that I don’t know the best way (I have 3 different solutions at the moment) to pass the VolleyError to the ViewModel if there is any.
I also want to remind that is not a best practice to have a LiveData in a Repository class.
There is some code that I have.
RequestUtils.java getUser :
public static void getUserInfos(Response.Listener<User> responseListener, Response.ErrorListener errorListener) {
GsonRequestByClass<User> gsonRequest = new GsonRequestByClass<>(Request.Method.GET, STRING_URL + "users/" + ID, User.class, responseListener, errorListener);
HTTPRequest.getInstance().addToQueue(gsonRequest);
}
The method is currently called in the onCreate method of my MainActivity.java, I want to use it in my UserRepository:
import static com.example.app.utils.RequestsUtils.getUser;
public class MainActivity extends AppCompatActivity {
//...
User user;
//...
protected void onCreate(Bundle savedInstanceState) {
//BEFORE
getUser(new Response.Listener<User>() {
@Override
public void onResponse(User response) {
user = response;
//do smth
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//There is a Toast here
}
});
//NOW
UserViewModel userModel = new ViewModelProvider(this).get(UserViewModel.class);
userModel.getUser1().observe(this, userResp->{
user = userResp;
//do smth
}); //how to handle error ?
}
}
UserViewModel.java
public class UserViewModel extends ViewModel {
private UserRepository userRepository;
private MutableLiveData<User> userLiveData;
public UserViewModel(){
userRepository = new UserRepository();
}
//solution 1
public LiveData<User> getUser1() {
if (userLiveData == null){
if (!userRepository.retrieveUserInfo1()){
userLiveData = new MutableLiveData<>();
userLiveData.setValue(userRepository.getUser());
} else {
//how indicate that there is an error for the Toast in activity ?
}
}
return userLiveData;
}
//solution 2
public LiveData<User> getUser2() {
if (userLiveData == null){
Response<User> reponse = userRepository.retrieveUserInfo2();
if (reponse.isSuccess()){
userLiveData.setValue(reponse.result);
} else {
//make smth with response.error
}
}
return userLiveData;
}
//solution 3
public LiveData<User> getUser3() {
if (userLiveData == null){
try {
userLiveData = userRepository.retrieveUserInfo3();
} catch (VolleyError e) {
throw new RuntimeException(e);
}
}
return userLiveData;
}
}
UserRepository.java
public class UserRepository {
private User user;
private boolean errorReq;
Response<User> responseReq;
public User getUser(){
return user;
}
//solution 1
public boolean retrieveUserInfo1() {
getUserInfos(new Response.Listener<User>() {
@Override
public void onResponse(User response) {
user = response;
errorReq = false;
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
errorReq = true;
}
});
return errorReq;
}
//solution 2
public Response<User> retrieveUserInfo2() {
getUserInfos(new Response.Listener<User>() {
@Override
public void onResponse(User response) {
responseReq = Response.success(response, null);
user = response;
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
responseReq = Response.error(error);
}
});
return responseReq;
}
//solution 3
public Response<User> retrieveUserInfo3() throws VolleyError {
getUserInfos(new Response.Listener<User>() {
@Override
public void onResponse(User response) {
errorReq = false;
user = response;
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
errorReq = true;
}
});
if (errorReq){
throw new VolleyError();
}
return user;
}
}
If anyone can help me ! This is my first question on SO, I hope I write it correctly and explain it well, tell me otherwise.
Ninny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.