既然说到封装,那就是对请求和返回结果进行统一处理
- 1 还是之前的post请求,我把返回结果写到ResultEntity这个类里面
public class ResultEntity {
/**
* 服务器返回数据一般就是{"code":0, "message":"123", "data:":泛型}
* 但是我们这边是测试,返回的数据格式如下,所以我们的结果处理只需要对info做处理就行
*
{"info": [{
"id": "5718",
"name": "\u300a\u5200\u950b\u4e4b\u5203\u300b\u5f00\u6d4b\u793c\u5305",
"gamename": "\u5200\u950b\u4e4b\u5203",
"icon": "http:\/\/i5.72g.com\/upload\/201606\/201606081043002654.jpg",
"remain": "48",
"gifttype": "1",
"consume": "0",
"content": "\u91d1\u5e01*10W\uff0c\u7fbd\u7075\u4e4b\u5fc3*500\uff0c\u9501\u8fb9\u7b26*2"
},
{
"id": "6686",
"name": "\u822a\u6d77\u738b\u542f\u822a\u6691\u671f\u793c\u5305",
"gamename": "\u822a\u6d77\u738b\u542f\u822a",
"icon": "http:\/\/i3.72g.com\/upload\/201606\/201606240949087788.jpg",
"remain": "3",
"gifttype": "1",
"consume": "0",
"content": "\u4ee5\u4e0b\u793c\u5305\u968f\u673a\u53d1\u653e\u4e00\u4e2a\uff1a\n1\uff1a100\u94bb \uff0c\u6d77\u76d7\u4fbf\u5f53*2 \uff0c\u5927\u94b1\u888b*2 \uff0c\u6676\u77f3\u5927\u793c\u5305*2 \uff0c\u8d85\u7ea7\u7ecf\u9a8c\u836f\u6c34*2\n2\uff1a\u94bb*188 \uff0c\u6d77\u76d7\u4fbf\u5f53*5 \uff0c\u5927\u94b1\u888b*5 \uff0c\u6676\u77f3\u5927\u793c\u5305*5 \uff0c\u8d85\u7ea7\u7ecf\u9a8c\u836f\u6c34*5\n3\uff1a\u8def\u98de\u788e\u7247*10 \uff0c\u94bb*88 \uff0c\u6d77\u76d7\u4fbf\u5f53*1 \uff0c\u5927\u94b1\u888b*1 \uff0c\u6676\u77f3\u5927\u793c\u5305*1\n4\uff1a\u94bb*188 \uff0c\u6d77\u76d7\u4fbf\u5f53*5 \uff0c\u5927\u94b1\u888b*5 \uff0c\u6676\u77f3\u5927\u793c\u5305*5 \uff0c\u8d85\u7ea7\u7ecf\u9a8c\u836f\u6c34*5\n5\uff1a100\u94bb \uff0c\u6d77\u76d7\u4fbf\u5f53*2 \uff0c\u5927\u94b1\u888b*2 \uff0c\u6676\u77f3\u5927\u793c\u5305*2 \uff0c\u8d85\u7ea7\u7ecf\u9a8c\u836f\u6c34*2\n6\uff1a\u94bb*188 \uff0c\u6d77\u76d7\u4fbf\u5f53*5 \uff0c\u5927\u94b1\u888b*5 \uff0c\u6676\u77f3\u5927\u793c\u5305*5 \uff0c\u8d85\u7ea7\u7ecf\u9a8c\u836f\u6c34*5\n7\uff1a\u8def\u98de\u788e\u7247*10 \uff0c\u94bb*88 \uff0c\u6d77\u76d7\u4fbf\u5f53*1 \uff0c\u5927\u94b1\u888b*1 \uff0c\u6676\u77f3\u5927\u793c\u5305*1"
}]}
*/
// private int code;
// private String message;
private T info;
public T getInfo() {
return info;
}
public void setInfo(T info) {
this.info = info;
}
}
因为返回的是一个数组,我们把数组中的单个提取到一个bean类里面,我命名为GameBean,代码如下:
public class GameBean implements Serializable{
/**
* id : 5718
* name : 《刀锋之刃》开测礼包
* gamename : 刀锋之刃
* icon : http://i5.72g.com/upload/201606/201606081043002654.jpg
* remain : 48
* gifttype : 1
* consume : 0
* content : 金币*10W,羽灵之心*500,锁边符*2
*/
private String id;
private String name;
private String gamename;
private String icon;
private String remain;
private String gifttype;
private String consume;
private String content;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGamename() {
return gamename;
}
public void setGamename(String gamename) {
this.gamename = gamename;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getRemain() {
return remain;
}
public void setRemain(String remain) {
this.remain = remain;
}
public String getGifttype() {
return gifttype;
}
public void setGifttype(String gifttype) {
this.gifttype = gifttype;
}
public String getConsume() {
return consume;
}
public void setConsume(String consume) {
this.consume = consume;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
- 2 去修改RetrofitService类getGameList方法,第一把返回结果改为List
,第二我们参数改为map(这个看个人爱好,你还可以接着用上篇中的RequestBody ):
public interface RetrofitService {
@FormUrlEncoded
@POST("app/gift/gift_list/")
Call>> getGameList(@FieldMap Map map);
}
- 3 返回结果要从ResponseBody转化为Bean,需要添加gson转换器,在gradle下面添加compile 'com.squareup.retrofit2:converter-gson:2.3.0',然后在初始化retrofit的时候添加一行代码,这样retrofit就可以把数据自动给转换为bean类了
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())//添加gson转换器
.build();
- 4 下面我们建一个ResponseCallback类对结果进行集中处理
public class ResponseCallback {
private Call> mCall;//拿到所有的call请求,集中处理
//返回结果处理后通过接口传给需要结果显示的地方,一般是activity,在activity中实现这个接口
public interface ResponseListener {
void onSuccess(T t);
void onFail(String error);
}
public ResponseCallback(Call call) {
mCall = call;
}
//对结果进行处理,说明一下在这里与okhttp不同的地方时,callback是在主线程直接可以更新ui,而okhttp则是工作线程需要通过handler等把结果传给主线程
public void handleResponse(final ResponseListener listener) {
mCall.enqueue(new Callback>() {
@Override
public void onResponse(Call> call, Response> response) {
if (response.isSuccessful() && response.errorBody() == null) {
listener.onSuccess((T) response.body().getInfo());
}
}
@Override
public void onFailure(Call> call, Throwable t) {
listener.onFail(t.getMessage().toString());
}
});
}
}
- 5 最后看一下activity中的调用代码
public class RetrofitActivity extends AppCompatActivity {
private String TAG = "RetrofitActivity";
private TextView tvResult;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.retrofit);
tvResult= (TextView) findViewById(R.id.tv_result);
//POST请求和上篇一样,创建请求体
try {
final Map map = new HashMap();
map.put("page", "1");
map.put("code", "news");
map.put("pageSize", "20");
map.put("parentid", "0");
map.put("type", "1");
Call>> call= HttpCenter.getInstance().service.getGameList(map);
new ResponseCallback>(call).handleResponse(new ResponseCallback.ResponseListener>() {
@Override
public void onSuccess(List gameBeens) {
if(gameBeens!=null&&gameBeens.size()>0)
tvResult.setText(gameBeens.get(0).toString());//textview显示请求结果
}
@Override
public void onFail(String error) {
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
其中HttpCenter类是将retrofit封装成单例,代码如下
public class HttpCenter {
public RetrofitService service;
public static final String BASE_URL = "http://zhushou.72g.com/";//BASE_URL请以/结尾
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public ResponseCallback responseCallback;
private static class HttpCenterHolder {
private static final HttpCenter INSTANCE = new HttpCenter();
}
public static final HttpCenter getInstance() {
return HttpCenterHolder.INSTANCE;
}
private HttpCenter() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())//添加gson转换器
.build();
service = retrofit.create(RetrofitService.class);
}
}
运行结果如下图:
所有代码均已上传到github: https://github.com/MrAllRight/HttpExample
本位参考: http://blog.csdn.net/gesanri/article/details/50938275