Created
June 1, 2020 04:19
-
-
Save JCarlosR/20a6770eb5ef3d8df312611baac8794d to your computer and use it in GitHub Desktop.
Retrofit 2 Example: Parsing dynamic JSON responses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.youtube.sorcjc.proyectoprofesionales.io; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import com.squareup.okhttp.OkHttpClient; | |
import com.squareup.okhttp.logging.HttpLoggingInterceptor; | |
import com.youtube.sorcjc.proyectoprofesionales.io.deserializers.LoginDeserializer; | |
import com.youtube.sorcjc.proyectoprofesionales.io.responses.LoginResponse; | |
import retrofit.GsonConverterFactory; | |
import retrofit.Retrofit; | |
public class ApiAdapter { | |
private static ApiService API_SERVICE; | |
public static ApiService getApiService() { | |
// Creating the interceptor, and setting the log level | |
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); | |
logging.setLevel(HttpLoggingInterceptor.Level.BODY); | |
// Adding the interceptor to a client | |
OkHttpClient httpClient = new OkHttpClient(); | |
httpClient.interceptors().add(logging); | |
if (API_SERVICE == null) { | |
Retrofit retrofit = new Retrofit.Builder() | |
.baseUrl("https://project.test.com/api/") | |
.addConverterFactory(buildGsonConverter()) | |
.client(httpClient) // <-- using the log level | |
.build(); | |
API_SERVICE = retrofit.create(ApiService.class); | |
} | |
return API_SERVICE; | |
} | |
private static GsonConverterFactory buildGsonConverter() { | |
GsonBuilder gsonBuilder = new GsonBuilder(); | |
// Adding custom deserializers | |
gsonBuilder.registerTypeAdapter(LoginResponse.class, new LoginDeserializer()); | |
Gson myGson = gsonBuilder.create(); | |
return GsonConverterFactory.create(myGson); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.youtube.sorcjc.proyectoprofesionales.io.deserializers; | |
import android.util.Log; | |
import com.google.gson.Gson; | |
import com.google.gson.JsonDeserializationContext; | |
import com.google.gson.JsonDeserializer; | |
import com.google.gson.JsonElement; | |
import com.google.gson.JsonObject; | |
import com.google.gson.JsonParseException; | |
import com.youtube.sorcjc.proyectoprofesionales.domain.UserAuthenticated; | |
import com.youtube.sorcjc.proyectoprofesionales.io.responses.LoginResponse; | |
import java.lang.reflect.Type; | |
public class LoginDeserializer implements JsonDeserializer<LoginResponse> { | |
@Override | |
public LoginResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { | |
Log.d("Test/Deserializer", "Using a custom deserializer for the Login request"); | |
Gson gson = new Gson(); | |
LoginResponse loginResponse = gson.fromJson(json, LoginResponse.class); | |
if (loginResponse.getStatus() == 1) { | |
// The full response as a json object | |
final JsonObject jsonObject = json.getAsJsonObject(); | |
// The response attribute in the JSON received | |
final JsonElement jsonElement = jsonObject.get("response"); | |
// The response will be parsed because the status was 1 | |
UserAuthenticated userAuthenticated = gson.fromJson(jsonElement, UserAuthenticated.class); | |
loginResponse.setResponse(userAuthenticated); | |
} | |
return loginResponse; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment