1

I am using Asycntask for handling my service. However, I want to use Retrofit and like to get some advice before moving on. My json services are like following. All of them have a result JSONObject and data(JSONObject or JSONArray). When I look at some tutorials, it says retrofit works with GSON and I have to convert my models to GSON format(http://www.jsonschema2pojo.org/). The thing I want to learn is, should I also add this result part of my services into my model. While using Asynctask, I am parsing result part and if the message is "ok", i start my data parsing. If message is not "ok", then I show an alert dialog with message. Can I get some advice about that?

 {
  result: {
  code: 0,
  message: "OK",
  dateTime: "20160204135212",
  },
 movie: [
  {
   name: "Movie 1",
   category: "drama"
  },
  {
   name: "Movie 2"
   category: "comedy"
  }
 ]
}
Figen Güngör
  • 12,169
  • 14
  • 66
  • 108

2 Answers2

0

Seems you need to use Interceptors.

Interceptors is the mechanism which allow to you do some work before use response. I mark the line, where you need to add transformation logic.

  public static class LoggingInterceptor implements Interceptor {
        @Override
        public com.squareup.okhttp.Response intercept(Chain chain) throws IOException {
            Log.i("LoggingInterceptor", "inside intercept callback");
            Request request = chain.request();
            long t1 = System.nanoTime();
            String requestLog = String.format("Sending request %s on %s%n%s",
                    request.url(), chain.connection(), request.headers());
            if (request.method().compareToIgnoreCase("post") == 0) {
                requestLog = "\n" + requestLog + "\n" + bodyToString(request);
            }
            Log.d("TAG", "request" + "\n" + requestLog);
            com.squareup.okhttp.Response response = chain.proceed(request);
            long t2 = System.nanoTime();

            String responseLog = String.format("Received response for %s in %.1fms%n%s",
                    response.request().url(), (t2 - t1) / 1e6d, response.headers());

            String bodyString = response.body().string();

            Log.d("TAG", "response only" + "\n" + bodyString);

            Log.d("TAG", "response" + "\n" + responseLog + "\n" + bodyString);

           // HERE YOU CAN ADD JSON DATA TO EXISTING RESPONSE

            return response.newBuilder()
                    .body(ResponseBody.create(response.body().contentType(), bodyString))
                    .build();

        }


        public static String bodyToString(final Request request) {
            try {
                final Request copy = request.newBuilder().build();
                final Buffer buffer = new Buffer();
                copy.body().writeTo(buffer);
                return buffer.readUtf8();
            } catch (final IOException e) {
                return "did not work";
            }
        }
    }
Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
0

Yes, this response from your service should be a model in your app. Retrofit will automatically serialize the json to java object in onResponse

@Override
public void onResponse(Response<Result> response){
    if(response.isSuccess()){
    Result result = response.body();
    if(result.getMessage().equals("OK")){
       //do something 
    }else{
       //show an alert dialog with message
    }
}
meda
  • 45,103
  • 14
  • 92
  • 122