Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ private JsonConvertor() {

public static Gson getInstance() {
if (gson == null) {
gson = new GsonBuilder()
.setPrettyPrinting()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(Date.class, new DateTypeAdapter())
.create();
gson = getDefaultGsonBuilder().create();
}
return gson;
}

public static void setInstance(Gson gson) {
JsonConvertor.gson = gson;
}

public static GsonBuilder getDefaultGsonBuilder() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a getter, it cannot be called "get...", maybe something like "createDefaultGsonBuilder" could be nice.

return new GsonBuilder()
.setPrettyPrinting()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(Date.class, new DateTypeAdapter());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.google.gson.Gson;
import com.readystatesoftware.chuck.Chuck;
import com.readystatesoftware.chuck.ChuckInterceptor;

import com.readystatesoftware.chuck.internal.support.JsonConvertor;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
Expand Down Expand Up @@ -53,11 +54,20 @@ public void onClick(View view) {
private OkHttpClient getClient(Context context) {
return new OkHttpClient.Builder()
// Add a ChuckInterceptor instance to your OkHttp client
.addInterceptor(new ChuckInterceptor(context))
.addInterceptor(createChuckInterceptor(context))
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.build();
}

@NonNull
private ChuckInterceptor createChuckInterceptor(Context context) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You add a method called "createChuckInterceptor", but it is not its only purpose. I see it also do some configuration of another class.
The configuration of JsonConverter could be done in the Application class, or within the onCreate of this activity.

Gson gson = JsonConvertor.getDefaultGsonBuilder()
.serializeNulls()
.create();
JsonConvertor.setInstance(gson);
return new ChuckInterceptor(context);
}

private void launchChuckDirectly() {
// Optionally launch Chuck directly from your own app UI
startActivity(Chuck.getLaunchIntent(this));
Expand Down