Retrofit 2カスタムGson解析
{
"code":0,
"message":" ",
"data":{" "}
}
または
{
"code":0,
"message":" ",
"data":[{},{},{}]
}
一般的には、インスタンス化するためにクラスを事前に作成します.
public class HttpData {
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;
}
}
Retrofit 2+Rxjava 2の使用に慣れている開発者は、Retrofit 2が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>() {
@Override
public void accept(List mHotGoodsWraps) throws Exception {
}
}, new Consumer() {
@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.
*
* 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 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 implements Converter {
private final Gson gson;
private final TypeAdapter adapter;
GsonResponseBodyConverter(Gson gson, TypeAdapter 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 extends ResourceSubscriber {
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>(mView) {
@Override
public void onNext(List 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 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 implements Converter {
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 adapter;
CustomGsonRequestBodyConverter(Gson gson, TypeAdapter 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 implements Converter {
private final Gson gson;
private final TypeAdapter adapter;
CustomGsonResponseBodyConverter(Gson gson, TypeAdapter 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();
}
}
}
GsonConverterFactoryの代わりにCustomGsonConverterFactoryを使えばいいです
mRetrofit = new Retrofit.Builder()
.client(mOkHttpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(CustomGsonConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.baseUrl(CashierApi.TEST_URL)
.build();