From ada1492cd21d22991c99a28d203fdff5bf4816ea Mon Sep 17 00:00:00 2001 From: sharifrafid Date: Fri, 6 Aug 2021 01:04:01 +0600 Subject: [PATCH] Changed Response Error Body Parsing Via Retrofit --- .../aether/repositories/RefreshRepository.java | 18 ++++++++++-------- .../utils/networking/NetworkingService.java | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/android/app/src/main/java/life/nsu/aether/repositories/RefreshRepository.java b/android/app/src/main/java/life/nsu/aether/repositories/RefreshRepository.java index 38be01be..06d1bfd9 100644 --- a/android/app/src/main/java/life/nsu/aether/repositories/RefreshRepository.java +++ b/android/app/src/main/java/life/nsu/aether/repositories/RefreshRepository.java @@ -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 { @@ -57,19 +60,18 @@ public void onResponse(@NonNull Call call, @NonNull Response 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(); } - } } diff --git a/android/app/src/main/java/life/nsu/aether/utils/networking/NetworkingService.java b/android/app/src/main/java/life/nsu/aether/utils/networking/NetworkingService.java index b90c7503..c4528853 100644 --- a/android/app/src/main/java/life/nsu/aether/utils/networking/NetworkingService.java +++ b/android/app/src/main/java/life/nsu/aether/utils/networking/NetworkingService.java @@ -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) {