Skip to content

Commit

Permalink
Merge pull request #13 from SharifRafid/master
Browse files Browse the repository at this point in the history
Android: Changed Response Error Body Parsing Via Retrofit
  • Loading branch information
samiurprapon authored Aug 5, 2021
2 parents 117bda7 + ada1492 commit de7bd2a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
import org.json.JSONObject;

import java.io.IOException;
import java.lang.annotation.Annotation;

import life.nsu.aether.utils.networking.NetworkingService;
import life.nsu.aether.utils.networking.responses.RefreshResponse;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Converter;
import retrofit2.Response;

public class RefreshRepository {
Expand Down Expand Up @@ -57,19 +60,18 @@ public void onResponse(@NonNull Call<RefreshResponse> call, @NonNull Response<Re
if (response.body() != null) {
refreshResponseMutableLiveData.postValue(response.body());
Log.d("refreshResponse", response.body().getMessage() +" " + response.body().isSuccess() + " " + response.body().getAccessToken());

}

if(response.errorBody() != null){
Converter<ResponseBody, RefreshResponse> converter
= NetworkingService.getInstance().retrofit
.responseBodyConverter(RefreshResponse.class, new Annotation[0]);
RefreshResponse errorResponse = null;
try {
JSONObject errorObject = new JSONObject(response.errorBody().string());
refreshResponseMutableLiveData.postValue(new RefreshResponse(false, "Login first!", ""));
Log.d("refreshResponse", " "+errorObject);

} catch (JSONException | IOException e) {
errorResponse = converter.convert(response.errorBody());
refreshResponseMutableLiveData.postValue(errorResponse);
} catch (IOException e) {
e.printStackTrace();
}

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class NetworkingService {

private static NetworkingService mInstance = null;
Retrofit retrofit;
public Retrofit retrofit;

public static synchronized NetworkingService getInstance() {
if (mInstance == null) {
Expand Down

0 comments on commit de7bd2a

Please sign in to comment.