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
    评论
Spring Boot与Retrofit2可以很好地结合使用,以方便地进行网络请求。下面是使用Spring Boot和Retrofit2的基本步骤: 1. 添加依赖:在项目的pom.xml文件中添加以下依赖: ```xml <dependencies> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Retrofit2 --> <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>retrofit</artifactId> <version>2.9.0</version> </dependency> <!-- Retrofit2 Gson Converter --> <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-gson</artifactId> <version>2.9.0</version> </dependency> </dependencies> ``` 2. 创建Retrofit实例:在Spring Boot的配置类中,使用`@Bean`注解创建一个Retrofit实例,并配置相关参数,如接口的基本URL、Gson转换器等。 ```java import okhttp3.OkHttpClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; @Configuration public class RetrofitConfig { @Bean public Retrofit retrofit() { OkHttpClient client = new OkHttpClient.Builder() // 可以添加自定义的OkHttpClient配置 .build(); return new Retrofit.Builder() .baseUrl("http://api.example.com/") // 设置接口的基本URL .client(client) .addConverterFactory(GsonConverterFactory.create()) // 设置Gson转换器 .build(); } } ``` 3. 创建API接口:定义一个接口,用于描述请求的方法和参数。 ```java import retrofit2.Call; import retrofit2.http.GET; public interface ApiService { @GET("users") Call<List<User>> getUsers(); } ``` 4. 使用Retrofit发起请求:在需要使用网络请求的地方,通过依赖注入的方式获取Retrofit实例,并调用接口方法发起请求。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import retrofit2.Call; import retrofit2.Response; import java.io.IOException; import java.util.List; @Service public class UserService { @Autowired private Retrofit retrofit; public List<User> getUsers() throws IOException { ApiService apiService = retrofit.create(ApiService.class); Call<List<User>> call = apiService.getUsers(); Response<List<User>> response = call.execute(); if (response.isSuccessful()) { return response.body(); } else { throw new IOException("请求失败:" + response.code()); } } } ``` 这样就可以在Spring Boot项目中使用Retrofit2进行网络请求了。注意要根据实际需求进行配置和调整,例如添加拦截器、设置超时时间等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值