All Downloads are FREE. Search and download functionalities are using the official Maven repository.

vip.ipav.okhttp.response.GsonResponseHandler Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
package vip.ipav.okhttp.response;

import com.google.gson.Gson;
import com.google.gson.internal.$Gson$Types;
import okhttp3.Response;
import okhttp3.ResponseBody;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Gson类型的回调接口
 * Created by tsy on 16/8/15.
 */
public abstract class GsonResponseHandler implements IResponseHandler {

    private Type mType;

    public GsonResponseHandler() {
        Type myclass = getClass().getGenericSuperclass();    //反射获取带泛型的class
        if (myclass instanceof Class) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameter = (ParameterizedType) myclass;      //获取所有泛型
        mType = $Gson$Types.canonicalize(parameter.getActualTypeArguments()[0]);  //将泛型转为type
    }

    private Type getType() {
        return mType;
    }

    @Override
    public final void onSuccess(final Response response) {
        ResponseBody responseBody = response.body();
        String responseBodyStr = "";

        try {
            responseBodyStr = responseBody.string();
        } catch (IOException e) {
            e.printStackTrace();
            onFailure(response.code(), "fail read response body");
            return;
        } finally {
            responseBody.close();
        }

        final String finalResponseBodyStr = responseBodyStr;

        try {
            Gson gson = new Gson();
            final T gsonResponse = (T) gson.fromJson(finalResponseBodyStr, getType());
            onSuccess(response.code(), gsonResponse);
        } catch (Exception e) {
            e.printStackTrace();
            onFailure(response.code(), "fail parse gson, body=" + finalResponseBodyStr);
        }
    }

    public abstract void onSuccess(int statusCode, T response);

    @Override
    public void onProgress(long currentBytes, long totalBytes) {

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy