package cn.muchinfo.rma.netManage.base;


import com.google.gson.internal.$Gson$Types;

import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.Callback;

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

import okhttp3.Call;
import okhttp3.Response;

public abstract class ResponseCallback<T> extends Callback<T> {


    public Type mType;

    public ResponseCallback() {
        mType = getSuperclassTypeParameter(getClass());
    }

    static Type getSuperclassTypeParameter(Class<?> subclass) {
        Type superclass = subclass.getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
        return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
    }

    @Override
    public T parseNetworkResponse(Response response, int id) throws Exception {
        return new Gson().fromJson(response.body().string(), mType);
    }

    //    @Override
//    public Object parseNetworkResponse(Response response, int id) throws Exception {
//        return new Gson().fromJson(response.body().string(), mType);
//    }

}
