Retrofit2自定义Gson解析

一般来说从服务端获取的数据格式是固定的:

{
    "code":0,
    "message":"成功",
    "data":{"测试内容"}
  }

或者

{
    "code":0,
    "message":"成功",
    "data":[{},{},{}]
  }

一般我们也会事先创建一个类来实例化

public class HttpData<T> {

    private static final int SUCCESS_CODE = 0;

    private int code;
    private String message;
    private T data;

    public boolean isSuccess() {
        return code == SUCCESS_CODE;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int mCode) {
        code = mCode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String mMessage) {
        message = mMessage;
    }

    public T getData() {
        return data;
    }

    public void setData(T mData) {
        data = mData;
    }
}

而习惯于使用Retrofit2 + Rxjava2的开发者知道Retrofit2可以通过addConverterFactory(GsonConverterFactory.create()) 来将后台返回的json数据自动转化为实体类

mRetrofit = new Retrofit.Builder()
                    .client(mOkHttpClient)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .baseUrl(CashierApi.TEST_URL)
                    .build();

这很方便,但是也有问题,就比如上面自定义的HttpData类,其中的data即可能是JSONObject,也可能是JSONArray,一般后台是沟通好的,返回什么格式都会事先定好,不会弄错。但是还是会出现一些意外的情况,当你使用JSONObject的时候返回JSONArray,或者相反的情况,这个时候就会发生错误。

同样的,每一次网络请求获取数据之后都要通过code值来判断是否请求成功,这样过于繁琐,那么是否能封装一下呢?

addDisposable(mDataManager.getHotGoodsList(shopId, presale, categoryId)
                .compose(RxUtils.rxSchedulerHelper())
                .compose(RxUtils.handleHttpData())
                .subscribe(new Consumer<List<HotGoodsWrap>>() {
                    @Override
                    public void accept(List<HotGoodsWrap> mHotGoodsWraps) throws Exception {

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable mThrowable) throws Exception {
                        
                    }
                }));

打开GsonConverterFactory

/*
 * Copyright (C) 2015 Square, Inc.
 *
 * 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 retrofit2.converter.gson;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * A {@linkplain Converter.Factory converter} which uses Gson for JSON.
 * <p>
 * Because Gson is so flexible in the types it supports, this converter assumes that it can handle
 * all types. If you are mixing JSON serialization with something else (such as protocol buffers),
 * you must {@linkplain Retrofit.Builder#addConverterFactory(Converter.Factory) add this instance}
 * last to allow the other converters a chance to see their types.
 */
public final class GsonConverterFactory extends Converter.Factory {
  /**
   * Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and
   * decoding from JSON (when no charset is specified by a header) will use UTF-8.
   */
  public static GsonConverterFactory create() {
    return create(new Gson());
  }

  /**
   * Create an instance using {@code gson} for conversion. Encoding to JSON and
   * decoding from JSON (when no charset is specified by a header) will use UTF-8.
   */
  @SuppressWarnings("ConstantConditions") // Guarding public API nullability.
  public static GsonConverterFactory create(Gson gson) {
    if (gson == null) throw new NullPointerException("gson == null");
    return new GsonConverterFactory(gson);
  }

  private final Gson gson;

  private GsonConverterFactory(Gson gson) {
    this.gson = gson;
  }

  @Override
  public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
      Retrofit retrofit) {
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
    return new GsonResponseBodyConverter<>(gson, adapter);
  }

  @Override
  public Converter<?, RequestBody> requestBodyConverter(Type type,
      Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
    return new GsonRequestBodyConverter<>(gson, adapter);
  }
}
可以看到responseBodyConverter和requestBodyConverter两个方法,这里我们主要看responseBodyConverter。打开GsonResponseBodyConverter类:
/*
 * Copyright (C) 2015 Square, Inc.
 *
 * 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 retrofit2.converter.gson;

import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import java.io.IOException;
import okhttp3.ResponseBody;
import retrofit2.Converter;

final class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
  private final Gson gson;
  private final TypeAdapter<T> adapter;

  GsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
    this.gson = gson;
    this.adapter = adapter;
  }

  @Override public T convert(ResponseBody value) throws IOException {
    JsonReader jsonReader = gson.newJsonReader(value.charStream());
    try {
      T result = adapter.read(jsonReader);
      if (jsonReader.peek() != JsonToken.END_DOCUMENT) {
        throw new JsonIOException("JSON document was not fully consumed.");
      }
      return result;
    } finally {
      value.close();
    }
  }
}

GsonResponseBodyConverter类很简单,在convert方法中的操作就是对后台数据的json解析,所以我们在这里面改就可以了,先上修改后的代码

@Override
    public T convert(ResponseBody value) throws IOException {
        try {
            String response = value.string();
            Log.i("CustomGsonResponse", "convert: " + response);
            JSONObject mJSONObject = null;
            try {
                mJSONObject = new JSONObject(response);
            } catch (JSONException mE) {
                mE.printStackTrace();
            }
            int code = mJSONObject.optInt("code", -1);
            if (code == Constants.NETWORD_SUCCESS_CODE) {
                MediaType mediaType = value.contentType();
                Charset charset = mediaType != null ? mediaType.charset(UTF_8) : UTF_8;
                InputStream inputStream = new ByteArrayInputStream(response.getBytes());
                JsonReader jsonReader = gson.newJsonReader(new InputStreamReader(inputStream, charset));
                T result = adapter.read(jsonReader);
                if (jsonReader.peek() != JsonToken.END_DOCUMENT) {
                    throw new JsonIOException("JSON document was not fully consumed.");
                }
                return result;
            } else {
                String message = mJSONObject.optString("message");
                value.close();
                throw new ApiException(code, message);
            }

        } finally {
            value.close();
        }
    }

简单解释一下,由于ResponseBody的数据只能被读取一次,所以在读取之后就要把它存储起来:

MediaType mediaType = value.contentType();
Charset charset = mediaType != null ? mediaType.charset(UTF_8) : UTF_8;
InputStream inputStream = new ByteArrayInputStream(response.getBytes());
JsonReader jsonReader = gson.newJsonReader(new InputStreamReader(inputStream, charset));

这就是这段代码的作用。

然后判断code的值是否是访问成功了

if (code == Constants.NETWORD_SUCCESS_CODE) {
    
} else {
    String message = mJSONObject.optString("message");
    value.close();
    throw new ApiException(code, message);
}

如果是的话就直接返回解析后的数据,如果不是的话就返回异常。

ApiException这个异常是我们自定义的:
public class ApiException extends RuntimeException {

    private int code;

    public ApiException(String message) {
        super(message);
    }

    public ApiException(int code, String message) {
        super(message);
        this.code = code;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int mCode) {
        code = mCode;
    }
}

我们在这里抛出异常,这个异常会在后面获得网络请求结果的onError中获得,然后我们再创建一个CommonSubscriber类:

public abstract class CommonSubscriber<T> extends ResourceSubscriber<T> {

    private BaseView mBaseView;
    private boolean isHideLoading = false;

    public CommonSubscriber() {

    }

    public CommonSubscriber(BaseView mBaseView) {
        this.mBaseView = mBaseView;
    }

    public CommonSubscriber(BaseView mBaseView, boolean isHideLoading) {
        this.mBaseView = mBaseView;
        this.isHideLoading = isHideLoading;
    }

    @Override
    public void onComplete() {
        if (mBaseView != null && isHideLoading) {
            mBaseView.hideLoadingView();
        }
    }

    @Override
    public void onError(Throwable mThrowable) {
        if (mBaseView == null) {
            return;
        }
        if (isHideLoading) {
            mBaseView.hideLoadingView();
        }

        if (mThrowable instanceof ApiException) {
            if (!StringUtils.isTextEmpty(mThrowable.getMessage())) {
                mBaseView.showErrorMsg(mThrowable.getMessage());
            }
        } else if (mThrowable instanceof HttpException) {
            mBaseView.showErrorMsg("数据加载失败");
        } else if (mThrowable instanceof SocketTimeoutException) {
            mBaseView.showErrorMsg("网络访问超时");
        } else {
            mThrowable.printStackTrace();
        }
    }
}

可以看到在onError方法中,我们判断异常的类型,然后通过不同的类型进行相关的处理,这样就能省下很大一块功夫,在使用的时候直接处理数据就可以了:

public void getHotGoodsList(int shopId, int presale, int categoryId) {
        addDisposable(mDataManager.getHotGoodsList(shopId, presale, categoryId)
                .compose(RxUtils.rxSchedulerHelper())
                .compose(RxUtils.handleHttpData())
                .subscribeWith(new CommonSubscriber<List<HotGoodsWrap>>(mView) {
                    @Override
                    public void onNext(List<HotGoodsWrap> mHotGoodsWraps) {
                        mView.onSuccessGetHotGoodsList(mHotGoodsWraps);
                    }
                }));
    }

当然,实际开发中有各种不同的情况,我们可以进行相关的处理来修改代码。最后附上要新建的几个Gson解析类:

CustomGsonConverterFactory:
package com.hongyue.cashregister.ui.widget.gsonconverter;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * Created by xkai on 2018/7/26.
 */

public class CustomGsonConverterFactory extends Converter.Factory {

    /**
     * Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and
     * decoding from JSON (when no charset is specified by a header) will use UTF-8.
     */
    public static CustomGsonConverterFactory create() {
        return create(new Gson());
    }

    /**
     * Create an instance using {@code gson} for conversion. Encoding to JSON and
     * decoding from JSON (when no charset is specified by a header) will use UTF-8.
     */
    @SuppressWarnings("ConstantConditions") // Guarding public API nullability.
    public static CustomGsonConverterFactory create(Gson gson) {
        if (gson == null) throw new NullPointerException("gson == null");
        return new CustomGsonConverterFactory(gson);
    }

    private final Gson gson;

    private CustomGsonConverterFactory(Gson gson) {
        this.gson = gson;
    }

    @Override
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
                                                            Retrofit retrofit) {
        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
        Log.i("main", "responseBodyConverter: " + type.toString());
        return new CustomGsonResponseBodyConverter<>(gson, adapter);
    }

    @Override
    public Converter<?, RequestBody> requestBodyConverter(Type type,
                                                          Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
        return new CustomGsonRequestBodyConverter<>(gson, adapter);
    }
}
CustomGsonRequestBodyConverter:
package com.hongyue.cashregister.ui.widget.gsonconverter;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.Buffer;
import retrofit2.Converter;

/**
 * Created by xkai on 2018/7/26.
 */

public class CustomGsonRequestBodyConverter<T> implements Converter<T, RequestBody> {

    private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
    private static final Charset UTF_8 = Charset.forName("UTF-8");

    private final Gson gson;
    private final TypeAdapter<T> adapter;

    CustomGsonRequestBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public RequestBody convert(T value) throws IOException {
        Buffer buffer = new Buffer();
        Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
        JsonWriter jsonWriter = gson.newJsonWriter(writer);
        adapter.write(jsonWriter, value);
        jsonWriter.close();
        return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
    }
}
CustomGsonResponseBodyConverter:
package com.hongyue.cashregister.ui.widget.gsonconverter;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.hongyue.cashregister.constant.Constants;
import com.hongyue.cashregister.model.bean.HttpData;
import com.hongyue.cashregister.model.http.exception.ApiException;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import okhttp3.MediaType;
import okhttp3.ResponseBody;
import retrofit2.Converter;

import static okhttp3.internal.Util.UTF_8;

/**
 * Created by xkai on 2018/7/26.
 */

public class CustomGsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final Gson gson;
    private final TypeAdapter<T> adapter;

    CustomGsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        try {
            String response = value.string();
            Log.i("CustomGsonResponse", "convert: " + response);
            JSONObject mJSONObject = null;
            try {
                mJSONObject = new JSONObject(response);
            } catch (JSONException mE) {
                mE.printStackTrace();
            }
            int code = mJSONObject.optInt("code", -1);
            if (code == Constants.NETWORD_SUCCESS_CODE) {
                MediaType mediaType = value.contentType();
                Charset charset = mediaType != null ? mediaType.charset(UTF_8) : UTF_8;
                InputStream inputStream = new ByteArrayInputStream(response.getBytes());
                JsonReader jsonReader = gson.newJsonReader(new InputStreamReader(inputStream, charset));
                T result = adapter.read(jsonReader);
                if (jsonReader.peek() != JsonToken.END_DOCUMENT) {
                    throw new JsonIOException("JSON document was not fully consumed.");
                }
                return result;
            } else {
                String message = mJSONObject.optString("message");
                value.close();
                throw new ApiException(code, message);
            }

        } finally {
            value.close();
        }
    }
}

使用的时候用CustomGsonConverterFactory替换GsonConverterFactory就可以了

mRetrofit = new Retrofit.Builder()
                    .client(mOkHttpClient)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .addConverterFactory(CustomGsonConverterFactory.create())
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .baseUrl(CashierApi.TEST_URL)
                    .build();

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Retrofit 是一个基于注解的、简化网络请求的库,它将网络请求和数据解析封装在一起,使得我们只需要关注业务逻辑即可。 下面我来介绍一下如何使用 Retrofit。 1. 添加依赖 首先,需要在项目的 build.gradle 文件中添加 Retrofit 的依赖: ``` implementation 'com.squareup.retrofit2:retrofit:2.9.0' ``` 如果需要使用 Retrofit 的 Gson 转换器,还需要添加以下依赖: ``` implementation 'com.squareup.retrofit2:converter-gson:2.9.0' ``` 2. 创建 Retrofit 实例 在使用 Retrofit 之前,需要先创建一个 Retrofit 实例。通常情况下,我们会在 Application 的 onCreate() 方法中创建 Retrofit 实例。 ``` public class MyApplication extends Application { private static Retrofit retrofit; @Override public void onCreate() { super.onCreate(); // 创建 Retrofit 实例 retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .addConverterFactory(GsonConverterFactory.create()) .build(); } public static Retrofit getRetrofit() { return retrofit; } } ``` 在上面的代码中,我们通过 Retrofit.Builder() 方法创建了一个 Retrofit 实例,并通过 baseUrl() 方法设置了服务器的地址。addConverterFactory() 方法用于设置数据解析器,这里我们使用 GsonConverterFactory。最后调用 build() 方法创建 Retrofit 实例。 3. 创建网络请求接口 接下来,我们需要创建一个网络请求接口,用于定义具体的网络请求方法。 ``` public interface ApiService { @GET("users/{username}") Call<User> getUser(@Path("username") String username); } ``` 在上面的代码中,我们通过 @GET 注解定义了一个 GET 请求方法,并在方法中使用了 @Path 注解来指定动态路径参数。方法的返回值是一个 Call 对象,它表示网络请求的响应结果。 4. 发起网络请求 最后,我们可以在业务逻辑中使用 Retrofit 实例和网络请求接口来发起网络请求。 ``` ApiService apiService = MyApplication.getRetrofit().create(ApiService.class); Call<User> call = apiService.getUser("octocat"); call.enqueue(new Callback<User>() { @Override public void onResponse(Call<User> call, Response<User> response) { if (response.isSuccessful()) { User user = response.body(); // 处理响应结果 } else { // 处理请求失败的情况 } } @Override public void onFailure(Call<User> call, Throwable t) { // 处理网络请求失败的情况 } }); ``` 在上面的代码中,我们通过 MyApplication.getRetrofit() 方法获取 Retrofit 实例,并使用 create() 方法创建 ApiService 的实例。然后调用 getUser() 方法发起网络请求,并在请求回调中处理响应结果。 以上就是 Retrofit 的基本使用方法。通过 Retrofit,我们可以轻松地进行网络请求,同时也可以自定义网络请求的拦截器等功能。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值