0

I had seen many examples regarding retrofit.And I had Done too most of like listview but this is something new which I am not finding easily.I am using retrofit2.Here it has same name details first is simple object and other have nested object and I am bit confuse how to parse it.It is alogin form First it checks details if it is one then it goes further.

[
 {
"title": "success",
"details": "1"
},
{
"title": "data",
"details": {
  "EMPLOYEE_ID": 5,
  "FIRST_NAME": "ABHISHEK",
  "LAST_NAME": "BARDOLIA"

  }
 }
]

Here is the Interface used in login purpose.

  public interface LoginInterface {
  @GET("Login")
  Call<List<ClsLogin>> Login(@Query("username") String username
        , @Query("password") String password
        , @Query("imei") String imei);
}

Model Class: public class ClsLogin {

@SerializedName("title")
String title;

@SerializedName("details")
String details;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDetails() {
    return details;
}

public void setDetails(String details) {
    this.details = details;
 }
}

Response Code:

public static void login(final LoginCallback logincallback, ClsLogin ObjLogin) {
    LoginInterface apiLoginService = ApiClient.getClient().create(LoginInterface.class);

    Call<List<ClsLogin>> call = apiLoginService.Login(
            ObjLogin.getUsername(),
            ObjLogin.getPassword(),
            ObjLogin.getImei());
    Log.e("response", String.valueOf(call.request().url()));


call.enqueue(new Callback<List<ClsLogin>>() {
    @Override
    public void onResponse(Call<List<ClsLogin>> call, Response<List<ClsLogin>> response) {
        Log.e("response", "onResponse ");
    }

    @Override
    public void onFailure(Call<List<ClsLogin>> call, Throwable t) {
        Log.e("response", t.getLocalizedMessage());
    }
});


}
Community
  • 1
  • 1

1 Answers1

0

you can see how GsonConverterFactory parsing data:

  @Override
  public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
  Retrofit retrofit) {
     TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
     return new GsonResponseBodyConverter<>(adapter);
}

how GsonResponseBodyConverter works:

    @Override public T convert(ResponseBody value) throws IOException {
    try {
      return adapter.fromJson(value.charStream());
    } finally {
      value.close();
    }
  }

so you can write your own ResponseBodyConverter to differentiate between data and the status code,you just have to override the convert method. because,GsonConverterFactory and GsonRequestBodyConverter are finnal class,you should copy them in your project.And you should code your Custom GsonResponseBodyConverter:

 final class CusGsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final TypeAdapter<T> adapter;
    private final Gson gson;

    CusGsonResponseBodyConverter(TypeAdapter<T> adapter) {
        this.adapter = adapter;
        this.gson = new Gson();
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        //get the response string data
        String response = value.string();

        //in there,you can check your data
        // for exp,you can catch the exception and return the entity with error flag
        //or just return the gson entity,like this:
        MediaType contentType = value.contentType();
        Charset charset = contentType != null ? contentType.charset(UTF_8) : UTF_8;
        InputStream inputStream = new ByteArrayInputStream(response.getBytes());
        Reader reader = new InputStreamReader(inputStream, charset);
        JsonReader jsonReader = gson.newJsonReader(reader);

        try {
            return adapter.read(jsonReader);
        } finally {
            value.close();
        }
    }
}
chris
  • 699
  • 4
  • 12
  • 35
zkywalker
  • 169
  • 1
  • 7