Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a library for parsing JSON #61

Open
bajtos opened this issue Dec 19, 2014 · 3 comments
Open

Use a library for parsing JSON #61

bajtos opened this issue Dec 19, 2014 · 3 comments

Comments

@bajtos
Copy link
Contributor

bajtos commented Dec 19, 2014

Few options:

@ainesophaur
Copy link

+1 for gson. Would love to have this feature

@firaskafri
Copy link

GSON

@jjhesk
Copy link

jjhesk commented Jul 20, 2016

why dont you just copy and consume the functionalities from gson??? it should be adding a conversion adapter. just like what retrofit2.0 did. yes i have done both.

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * Created by jjyu on 2/7/15.
 */
public class Client2 {
    public final static String LOG_TAG = "hbsdk.log";
    protected Gson gsonsetup;
    protected Context context;
    protected boolean cache_supported;
    protected Retrofit apicreator;
    protected OkHttpClient client2 = new OkHttpClient();
    protected String base_url;
    protected apiInterceptor mApiInterceptor;

    protected void jsoncreate() {
        gsonsetup = new GsonBuilder()
                .setDateFormat(Constant.DATE_FORMAT)
                .registerTypeAdapterFactory(new GsonFactory.NullStringToEmptyAdapterFactory())
                .registerTypeAdapter(String.class, new WordpressConversion())
                .setExclusionStrategies(new RealmExclusion())
                .create();
    }


    protected void registerJsonAdapter() {
        // Add the interceptor to OkHttpClient
        apicreator = new Retrofit.Builder()
                .baseUrl(base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .client(createClient())
                .build();
    }


    protected OkHttpClient createClient() {
        mApiInterceptor = new apiInterceptor();
        OkHttpClient.Builder cb = new OkHttpClient.Builder();
        cb.addInterceptor(mApiInterceptor);
        cb.connectTimeout(2, TimeUnit.MINUTES);
        final HttpLoggingInterceptor logger = new HttpLoggingInterceptor();
        if (BuildConfig.DEBUG) {
            logger.setLevel(HttpLoggingInterceptor.Level.BODY);
        } else {
            logger.setLevel(HttpLoggingInterceptor.Level.NONE);
        }

        logger.setLevel(HttpLoggingInterceptor.Level.BODY);
        cb.addNetworkInterceptor(logger);
        return cb.build();
    }

    public apiInterceptor getInterceptor() {
        return mApiInterceptor;
    }

    public Client2(Context context, String base) {
        cache_supported = false;
        this.base_url = base;
        this.context = context;
        jsoncreate();
        registerJsonAdapter();
    }

    /**
     * Dangerous interceptor that rewrites the server's cache-control header.
     */
    protected static final Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = new Interceptor() {
        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());
            return originalResponse.newBuilder()
                    .header("Cache-Control", "max-age=60")
                    .build();
        }
    };

    public Retrofit getCreator() {
        return apicreator;
    }

    public Gson getGson() {
        return gsonsetup;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants