forked from hongyangAndroid/okhttputils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[add][what]增加一个泛型的序列化Callback (sample项目中有一个Json序列化的例子)
[why]避免每种类型的返回对象都需要新建相对应的Callback类
- Loading branch information
江佳铨
committed
Jun 23, 2016
1 parent
a209026
commit 1d717d1
Showing
4 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
okhttputils/src/main/java/com/zhy/http/okhttp/callback/GenericsCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.zhy.http.okhttp.callback; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.ParameterizedType; | ||
|
||
import okhttp3.Response; | ||
|
||
/** | ||
* Created by JimGong on 2016/6/23. | ||
*/ | ||
|
||
public abstract class GenericsCallback<T> extends Callback<T> { | ||
IGenericsSerializator mGenericsSerializator; | ||
|
||
public GenericsCallback(IGenericsSerializator serializator) { | ||
mGenericsSerializator = serializator; | ||
} | ||
|
||
@Override | ||
public T parseNetworkResponse(Response response, int id) throws IOException { | ||
String string = response.body().string(); | ||
Class<T> entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; | ||
if (entityClass == String.class) { | ||
return (T) string; | ||
} | ||
T bean = mGenericsSerializator.transform(string, entityClass); | ||
return bean; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
okhttputils/src/main/java/com/zhy/http/okhttp/callback/IGenericsSerializator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.zhy.http.okhttp.callback; | ||
|
||
/** | ||
* Created by JimGong on 2016/6/23. | ||
*/ | ||
public interface IGenericsSerializator { | ||
<T> T transform(String response, Class<T> classOfT); | ||
} |
15 changes: 15 additions & 0 deletions
15
sample-okhttp/src/main/java/com/zhy/sample_okhttp/JsonGenericsSerializator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.zhy.sample_okhttp; | ||
|
||
import com.google.gson.Gson; | ||
import com.zhy.http.okhttp.callback.IGenericsSerializator; | ||
|
||
/** | ||
* Created by JimGong on 2016/6/23. | ||
*/ | ||
public class JsonGenericsSerializator implements IGenericsSerializator { | ||
Gson mGson = new Gson(); | ||
@Override | ||
public <T> T transform(String response, Class<T> classOfT) { | ||
return mGson.fromJson(response, classOfT); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters