com.lwx.ysnewdemo.callback.JsonConvert.java Source code

Java tutorial

Introduction

Here is the source code for com.lwx.ysnewdemo.callback.JsonConvert.java

Source

/*
 * Copyright 2016 jeasonlzy(?)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lwx.ysnewdemo.callback;

import com.google.gson.stream.JsonReader;
import com.lwx.ysnewdemo.model.SimpleResponse;
import com.lwx.ysnewdemo.rx.LwxResponse;
import com.lwx.ysnewdemo.utils.Convert;
import com.lzy.okgo.convert.Converter;

import org.json.JSONArray;
import org.json.JSONObject;

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

import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * ================================================
 *     jeasonlzy?Github?https://github.com/jeasonlzy
 *     1.0
 * 16/9/11
 * ??    
 * ?
 * ================================================
 */
public class JsonConvert<T> implements Converter<T> {

    private Type type;
    private Class<T> clazz;

    public JsonConvert() {
    }

    public JsonConvert(Type type) {
        this.type = type;
    }

    public JsonConvert(Class<T> clazz) {
        this.clazz = clazz;
    }

    /**
     * ????ui
     * ?? response ?onSuccess??
     * ????,?,??,??
     */
    @Override
    public T convertResponse(Response response) throws Throwable {

        // ???????????
        // ???????????
        // ???????????

        // ?????: https://github.com/jeasonlzy/okhttp-OkGo/wiki/JsonCallback
        // ?????: https://github.com/jeasonlzy/okhttp-OkGo/wiki/JsonCallback
        // ?????: https://github.com/jeasonlzy/okhttp-OkGo/wiki/JsonCallback

        if (type == null) {
            if (clazz == null) {
                // ?????
                Type genType = getClass().getGenericSuperclass();
                type = ((ParameterizedType) genType).getActualTypeArguments()[0];
            } else {
                return parseClass(response, clazz);
            }
        }

        if (type instanceof ParameterizedType) {
            return parseParameterizedType(response, (ParameterizedType) type);
        } else if (type instanceof Class) {
            return parseClass(response, (Class<?>) type);
        } else {
            return parseType(response, type);
        }
    }

    private T parseClass(Response response, Class<?> rawType) throws Exception {
        if (rawType == null)
            return null;
        ResponseBody body = response.body();
        if (body == null)
            return null;
        JsonReader jsonReader = new JsonReader(body.charStream());

        if (rawType == String.class) {
            //noinspection unchecked
            return (T) body.string();
        } else if (rawType == JSONObject.class) {
            //noinspection unchecked
            return (T) new JSONObject(body.string());
        } else if (rawType == JSONArray.class) {
            //noinspection unchecked
            return (T) new JSONArray(body.string());
        } else {
            T t = Convert.fromJson(jsonReader, rawType);
            response.close();
            return t;
        }
    }

    private T parseType(Response response, Type type) throws Exception {
        if (type == null)
            return null;
        ResponseBody body = response.body();
        if (body == null)
            return null;
        JsonReader jsonReader = new JsonReader(body.charStream());

        // ? new JsonCallback<?JavaBean>(this)
        T t = Convert.fromJson(jsonReader, type);
        response.close();
        return t;
    }

    private T parseParameterizedType(Response response, ParameterizedType type) throws Exception {
        if (type == null)
            return null;
        ResponseBody body = response.body();
        if (body == null)
            return null;
        JsonReader jsonReader = new JsonReader(body.charStream());

        Type rawType = type.getRawType(); // 
        Type typeArgument = type.getActualTypeArguments()[0]; // ?
        if (rawType != LwxResponse.class) {
            // ? new JsonCallback<BaseBean<JavaBean>>(this)
            T t = Convert.fromJson(jsonReader, type);
            response.close();
            return t;
        } else {
            if (typeArgument == Void.class) {
                // ? new JsonCallback<LzyResponse<Void>>(this)
                SimpleResponse simpleResponse = Convert.fromJson(jsonReader, SimpleResponse.class);
                response.close();
                //noinspection unchecked
                return (T) simpleResponse.toLwxResponse();
            } else {
                // ? new JsonCallback<LzyResponse<JavaBean>>(this)
                LwxResponse lzyResponse = Convert.fromJson(jsonReader, type);
                response.close();
                int code = lzyResponse.errcode;
                //0??
                //????
                if (code == 0) {
                    //noinspection unchecked
                    return (T) lzyResponse;
                } else if (code == 104) {
                    throw new IllegalStateException("??");
                } else if (code == 105) {
                    throw new IllegalStateException("??");
                } else {
                    //??onError??
                    throw new IllegalStateException(
                            "?" + code + "?" + lzyResponse.message);
                }
            }
        }
    }
}