前言
在这一篇文章中,主要讲一下如何使用Gson解释服务器返回的具有固定格式的数据。
分析
服务器:在本地使用nodejs的express框架建立的简单服务器。它返回了的数据如下:
1 2 3 4 5
| var testArrayStr = "{\"data\": [{\"cnName\": \"jakewharton\",\"age\": 13,\"IsBoy\": true}, {\"cnName\": \"小红\",\"age\": 24,\"IsBoy\": false}],\"msg\": \"\",\"status\": 200}";
var testObjStr = "{\"data\": {\"cnName\": \"小红\",\"age\": 24,\"IsBoy\": false},\"msg\": \"\",\"status\": 200}";
res.end(testObjStr);
|
我们可以和服务器约定返回的格式模版如下,他们的主要区别是data,可以是对象或者对象的数组形式。
定义解释data为对象的模板:
1 2 3 4 5
| public class BaseObjectResult<T> { public T data; public String msg; public int status; }
|
定义解释data为数组的模版:
1 2 3 4 5
| public class BaseArrayResult<T> { public List<T> data; public String msg; public int status; }
|
实体对象:
TestData.java
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class TestData { public String cnName; public int age; public boolean IsBoy;
@Override public String toString() { return "testData:" + "cnName=" + this.cnName + " " + "age=" + this.age + " " + "IsBogy=" + this.IsBoy; } }
|
使用retrofit和gson解释
自定义Converter.Factory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class DecodeConverterFactory extends Converter.Factory {
public static DecodeConverterFactory create() { return create(new Gson()); }
public static DecodeConverterFactory create(Gson gson) { return new DecodeConverterFactory(gson); }
private final Gson gson;
private DecodeConverterFactory(Gson gson) { if (gson == null) throw new NullPointerException("gson == null"); this.gson = gson; }
@Override public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type)); return new CustomResponseBodyConverter<>(adapter, type); }
@Override public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) { TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type)); return new DecodeRequestBodyConverter<>(gson, adapter); }
}
|
CustomResponseBodyConverter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public class CustomResponseBodyConverter<T> implements Converter<ResponseBody, T> { private final TypeAdapter<T> adapter; private Type mType;
public CustomResponseBodyConverter(TypeAdapter<T> adapter, Type type) { this.adapter = adapter; this.mType = type; }
@Override public T convert(ResponseBody value) throws IOException { //解密字符串 if(mType == String.class) { return (T) value.string(); } else { } } }
|
DecodeRequestBodyConverter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public class DecodeRequestBodyConverter<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; DecodeRequestBodyConverter(Gson gson,TypeAdapter<T> adapter){ this.gson = gson; this.adapter = adapter; } @Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); //value.toString() Writer writer = new OutputStreamWriter(buffer.outputStream(),UTF_8); JsonWriter jsonWriter = gson.newJsonWriter(writer); adapter.write(jsonWriter,value); jsonWriter.flush(); return RequestBody.create(MEDIA_TYPE,buffer.readByteString()); }
}
|
开始使用:
TestDataApi.java
1 2 3 4
| public interface TestDataApi { @GET("/") Call<BaseObjectResult<TestData>> getArrayData(); }
|
当data为对象时:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://192.168.20.168:3000") .addConverterFactory(DecodeConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build();
TestDataApi testDataApi = retrofit.create(TestDataApi.class); Call<BaseObjectResult<TestData>> resultCall = testDataApi.getArrayData(); resultCall.enqueue(new Callback<BaseObjectResult<TestData>>() { @Override public void onResponse(Call<BaseObjectResult<TestData>> call, Response<BaseObjectResult<TestData>> response) { if(response.isSuccessful()) { if(response.body() != null) { TestData testData = response.body().data; Log.d("hyj", "msg=" + response.body().msg + " " + "status=" + response.body().status + " " + testData.toString()); } }
}
@Override public void onFailure(Call<BaseObjectResult<TestData>> call, Throwable t) { ToastUtil.showShort(mContext, t.getMessage()); } });
|
输出的结果是:
04-08 16:04:56.053 31894-31894/com.zhangsunyucong.chanxa.testproject D/hyj: msg= status=200 testData:cnName=小红 age=24 IsBogy=false
当data为数组时:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://192.168.20.168:3000") .addConverterFactory(DecodeConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build();
TestDataApi testDataApi = retrofit.create(TestDataApi.class); Call<BaseArrayResult<TestData>> resultCall = testDataApi.getArrayData(); resultCall.enqueue(new Callback<BaseArrayResult<TestData>>() { @Override public void onResponse(Call<BaseArrayResult<TestData>> call, Response<BaseArrayResult<TestData>> response) { if(response.isSuccessful()) { if(response.body() != null) { List<TestData> testData = response.body().data; if(testData != null) { for(int i = 0; i < testData.size(); i++) { Log.d("hyj", "msg=" + response.body().msg + " " + "status=" + response.body().status + " " + testData.get(i).toString()); } } } } }
@Override public void onFailure(Call<BaseArrayResult<TestData>> call, Throwable t) { ToastUtil.showShort(mContext, t.getMessage()); } });
|
输出的结果是:
04-08 16:11:44.703 32440-32440/com.zhangsunyucong.chanxa.testproject D/hyj: msg= status=200 testData:cnName=jakewharton age=13 IsBogy=true
04-08 16:11:44.703 32440-32440/com.zhangsunyucong.chanxa.testproject D/hyj: msg= status=200 testData:cnName=小红 age=24 IsBogy=false
手动解释####
关键的代码是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| private ParameterizedType type(final Class raw, final Type... args) { return new ParameterizedType() { public Type getRawType() { return raw; }
public Type[] getActualTypeArguments() { return args; }
public Type getOwnerType() { return null; } }; }
|
当data返回的是对象时:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| TestDataApi testDataApi = retrofit.create(TestDataApi.class); Call<String> resultCall = testDataApi.getArrayData(); resultCall.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { if(response.isSuccessful()) { if(response.body() != null) { String testDataStr = response.body(); Gson gson = new Gson();
BaseObjectResult<TestData> testData = gson.fromJson(testDataStr, type(BaseObjectResult.class, TestData.class));
Log.d("hyj", "msg=" + testData.msg + " " + "status=" + testData.status + " " + testData.data.toString()); } } }
@Override public void onFailure(Call<String> call, Throwable t) { ToastUtil.showShort(mContext, t.getMessage()); } });
|
当返回的data是数组时:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| TestDataApi testDataApi = retrofit.create(TestDataApi.class); Call<String> resultCall = testDataApi.getArrayData(); resultCall.enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { if(response.isSuccessful()) { if(response.body() != null) { String testDataStr = response.body(); Gson gson = new Gson();
BaseArrayResult<TestData> testData = gson.fromJson(testDataStr, type(BaseArrayResult.class, TestData.class));
if(testData != null && testData.data != null) { for(int i = 0; i < testData.data.size(); i++) { Log.d("hyj", "msg=" + testData.msg + " " + "status=" + testData.status + " " + testData.data.get(i).toString()); } } } } }
@Override public void onFailure(Call<String> call, Throwable t) { ToastUtil.showShort(mContext, t.getMessage()); } });
|
它们返回的结果和第一种方法的返回结果是一样的。