Search in sources :

Example 1 with NewsResponse

use of com.lzy.demo.model.NewsResponse in project okhttp-OkGo by jeasonlzy.

the class NewsCallback method convertSuccess.

/**
     * 这里的数据解析是根据 http://apistore.baidu.com/apiworks/servicedetail/688.html 返回的数据来写的
     * 实际使用中,自己服务器返回的数据格式和上面网站肯定不一样,所以以下是参考代码,根据实际情况自己改写
     */
@Override
public T convertSuccess(Response response) throws Exception {
    //以下代码是通过泛型解析实际参数,泛型必须传
    Type genType = getClass().getGenericSuperclass();
    Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
    Type type = params[0];
    if (!(type instanceof ParameterizedType))
        throw new IllegalStateException("没有填写泛型参数");
    JsonReader jsonReader = new JsonReader(response.body().charStream());
    Type rawType = ((ParameterizedType) type).getRawType();
    if (rawType == NewsResponse.class) {
        NewsResponse newsResponse = Convert.fromJson(jsonReader, type);
        if (newsResponse.showapi_res_code == 0) {
            response.close();
            //noinspection unchecked
            return (T) newsResponse;
        } else {
            response.close();
            throw new IllegalStateException(newsResponse.showapi_res_error);
        }
    } else {
        response.close();
        throw new IllegalStateException("基类错误无法解析!");
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonReader(com.google.gson.stream.JsonReader) NewsResponse(com.lzy.demo.model.NewsResponse)

Example 2 with NewsResponse

use of com.lzy.demo.model.NewsResponse in project okhttp-OkGo by jeasonlzy.

the class NewsConvert method convertSuccess.

@Override
public T convertSuccess(Response response) throws Exception {
    //以下代码是通过泛型解析实际参数,泛型必须传
    Type genType = getClass().getGenericSuperclass();
    Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
    Type type = params[0];
    if (!(type instanceof ParameterizedType))
        throw new IllegalStateException("没有填写泛型参数");
    JsonReader jsonReader = new JsonReader(response.body().charStream());
    Type rawType = ((ParameterizedType) type).getRawType();
    if (rawType == NewsResponse.class) {
        NewsResponse newsResponse = Convert.fromJson(jsonReader, type);
        if (newsResponse.showapi_res_code == 0) {
            response.close();
            //noinspection unchecked
            return (T) newsResponse;
        } else {
            response.close();
            throw new IllegalStateException(newsResponse.showapi_res_error);
        }
    } else {
        response.close();
        throw new IllegalStateException("基类错误无法解析!");
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonReader(com.google.gson.stream.JsonReader) NewsResponse(com.lzy.demo.model.NewsResponse)

Aggregations

JsonReader (com.google.gson.stream.JsonReader)2 NewsResponse (com.lzy.demo.model.NewsResponse)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2