org.intermine.app.net.request.JsonGetRequest.java Source code

Java tutorial

Introduction

Here is the source code for org.intermine.app.net.request.JsonGetRequest.java

Source

package org.intermine.app.net.request;

/*
 * Copyright (C) 2015 InterMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;

import org.apache.commons.lang3.ArrayUtils;
import org.intermine.app.util.Strs;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import java.util.Map;

public class JsonGetRequest<T> extends GetRequest<T> {
    public static final String TAG = JsonGetRequest.class.getSimpleName();

    public final static String CONTENT_TYPE = "application";
    public final static String CONTENT_SUBTYPE = "json";

    public final static String DEFAULT_ROOT = "params";

    private String mOutWrapper;

    public JsonGetRequest(Class<T> clazz, Context ctx, String url, Map<String, ?> params) {
        super(clazz, ctx, url, params);
    }

    public Gson getMapper() {
        return new Gson();
    }

    @Override
    public HttpHeaders getHeaders() {
        HttpHeaders headers = super.getHeaders();
        headers.setContentType(new MediaType(CONTENT_TYPE, CONTENT_SUBTYPE));

        return headers;
    }

    @Override
    public T loadDataFromNetwork() throws Exception {
        Map<String, ?> params = getUrlParams();
        String uri = getUrl();
        Gson mapper = getMapper();

        Log.i(TAG, "GET: " + expandQuery(uri, params));
        Log.i(TAG, "H: " + getHeaders());

        byte[] bytes = loadBytes(uri, params);
        T result = null;

        if (!ArrayUtils.isEmpty(bytes)) {
            String res = new String(bytes);
            Log.i(TAG, "RES: " + res);

            if (!Strs.isNullOrEmpty(mOutWrapper)) {
                Map<String, Object> rooted = mapper.fromJson(res, Map.class);
                String inner = mapper.toJson(rooted.get(mOutWrapper));

                result = mapper.fromJson(inner, getResultType());
            } else {
                result = mapper.fromJson(res, getResultType());
            }
        }

        Log.i(TAG, "RES: " + result);

        return result;
    }

    public String getOutWrapper() {
        return mOutWrapper;
    }

    public void setOutWrapper(String wrapper) {
        mOutWrapper = wrapper;
    }
}