Answer the question
In order to leave comments, you need to log in
What can you say about this code?
Here is the code to send a request to perform a simple action - removing/adding the "favorite" tag:
protected void toggleFavorite() {
if (favoriteRequest != null && favoriteRequest.executing()) {
favoriteRequest.cancel();
}
if (favoriteRequest == null) {
favoriteRequest = new BouquetRequest(getApplicationContext());
favoriteRequest.setListener(new BouquetRequest.Listener()
{
@Override
public void success(String message, @Nullable Object data) {
try {
if (data == null) {
favorites.delete(favorite);
favorite = null;
} else {
favorite = Favorite.fromJson((JSONObject) data);
favorites.create(favorite);
}
} catch (IOException|SQLException e) {
Toast.makeText(getApplicationContext(), R.string.unknown_error,
Toast.LENGTH_SHORT).show();
}
}
@Override
public void error(String error, String code, @Nullable Object data) {
Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show();
}
});
favoriteRequest.before(new BouquetRequest.BeforeCallback()
{
@Override
public void run(StringRequest request) {
mFavoriteButton.setEnabled(false);
}
});
favoriteRequest.after(new BouquetRequest.AfterCallback()
{
@Override
public void run(@Nullable NetworkResponse response) {
mFavoriteButton.setEnabled(true);
}
});
}
favoriteRequest
.setMethod(favorite == null ? BouquetRequest.POST : BouquetRequest.DELETE)
.setAction(favorite == null ? "favorite" : "favorite/:id");
if (favorite != null) {
favoriteRequest.bindParam("id", favorite.id);
}
favoriteRequest.execute();
}
Answer the question
In order to leave comments, you need to log in
Terrible, I would throw it all away, take Retrofit, RxJava, take it from the fragment / activity to the presenter and live happily ever after.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question