1.介绍
Retrofit是Square提供的开源产品,为Android平台的应用提供一个类型安全的REST客户端。它是基于注解,提供 JSON to POJO(Plain Ordinary Java Object ,简单 Java 对象),POJO to JSON,网络请求(POST,GET, PUT,DELETE 等)封装。
Retrofit 虽然是一套注解形的网络请求封装库,但是它的强大在于让代码结构更给为清晰,它可以直接解析JSON数据变成JAVA对象,甚至支持回调操作,处理不同的结果。
官方文档地址:http://square.github.io/retrofit/
github地址:https://github.com/square/retrofit
2.基本使用
添加依赖(as)
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
compile 'com.google.code.gson:gson:2.3'
数据格式,就拿我们公司的登录数据,json格式略复杂:
{
"data": {
"userInfo": {
"qq": "123124143213",
"address": "xxxx",
"salt": "xxx",
"securityQuestion": [
1,
1,
"您母亲的姓名是什么?",
2,
"您父亲的姓名是什么?",
3,
"您配偶的姓名是什么?"
],
"sex": 0,
"mobile": "xxxx",
"wechat": "xxxx",
"photo": "default10.jpg",
"point": "",
"uid": 127,
"name": "pansheng",
"userType": 3,
"village": "湖北武汉",
"email": "xxx@qq.com",
"mobileAbbr": "177****6683",
"username": "xxxx"
},
"sqInfo": [
[
1,
"您母亲的姓名是什么?"
],
[
2,
"您父亲的姓名是什么?"
],
[
3,
"您配偶的姓名是什么?"
],
[
4,
"您的出生地是哪里?"
],
[
5,
"您高中班主任的名字是什么?"
],
[
6,
"您初中班主任的名字是什么?"
],
[
7,
"您小学班主任的名字是什么?"
],
[
8,
"您的小学校名是什么?"
],
[
9,
"您的学号(或工号)是什么?"
],
[
10,
"您父亲的生日是什么?"
],
[
11,
"您母亲的生日是什么?"
],
[
12,
"您配偶的生日是什么?\r\n"
],
[
13,
"您哪一年高中毕业?"
],
[
14,
"您哪一年初中毕业?"
],
[
15,
"您的初恋是谁?"
]
],
"groupInfo": [
[
16,
"TCL-罗格朗2017跨年团购专场(12月31日-1月2日)",
"e0de0b0aa7424f33a6010f15a6b2bdcd.jpg"
],
[
18,
"TCL-罗格朗第二季团购专场(6月28日-6月30日)",
"ae025a08c3644275980f3ed754bd059c.jpg"
],
[
1614,
"王力安全门第三季团购专场(9月8日-9月10日)",
"413f93bb714e4a37bcf9b958098a6941.jpg"
]
],
"resourceInfo": {
"user_photo_path": "/images/img/",
"group_logo_path": "/group/"
}
},
"message": "登陆成功!",
"status": "0"
}
LoginInfo实体类如下:
public class LoginInfo {
private String message;
private int status;
private ResultBean data;
@Override
public String toString() {
return "LoginInfo{" +
"message='" + message + '\'' +
", status=" + status +
", data=" + data +
'}';
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public ResultBean getData() {
return data;
}
public void setData(ResultBean data) {
this.data = data;
}
public static class ResultBean{
private UserInfo userInfo;
private ResourceInfo resourceInfo;
private List<List<String>> sqInfo;
private List<List<String>> groupInfo;
@Override
public String toString() {
return "ResultBean{" +
"userInfo=" + userInfo +
", resourceInfo=" + resourceInfo +
", sqInfo=" + sqInfo +
", groupInfo=" + groupInfo +
'}';
}
public UserInfo getUserInfo() {
return userInfo;
}
public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
}
public ResourceInfo getResourceInfo() {
return resourceInfo;
}
public void setResourceInfo(ResourceInfo resourceInfo) {
this.resourceInfo = resourceInfo;
}
public List<List<String>> getSqInfo() {
return sqInfo;
}
public void setSqInfo(List<List<String>> sqInfo) {
this.sqInfo = sqInfo;
}
public List<List<String>> getGroupInfo() {
return groupInfo;
}
public void setGroupInfo(List<List<String>> groupInfo) {
this.groupInfo = groupInfo;
}
}
public static class UserInfo{
private String qq;
private String address;
private String salt;
private List<String> securityQuestion;
private int sex;
private String mobile;
private String wechat;
private String photo;
private String point;
private int uid;
private String name;
private int userType;
private String village;
private String email;
private String mobileAbbr;
private String username;
@Override
public String toString() {
return "UserInfo{" +
"qq='" + qq + '\'' +
", address='" + address + '\'' +
", salt='" + salt + '\'' +
", securityQuestion=" + securityQuestion +
", sex=" + sex +
", mobile='" + mobile + '\'' +
", wechat='" + wechat + '\'' +
", phone='" + photo + '\'' +
", point='" + point + '\'' +
", uid=" + uid +
", name='" + name + '\'' +
", userType=" + userType +
", village='" + village + '\'' +
", email='" + email + '\'' +
", mobileAbbr='" + mobileAbbr + '\'' +
", username='" + username + '\'' +
'}';
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
public List<String> getSecurityQuestion() {
return securityQuestion;
}
public void setSecurityQuestion(List<String> securityQuestion) {
this.securityQuestion = securityQuestion;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getWechat() {
return wechat;
}
public void setWechat(String wechat) {
this.wechat = wechat;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String phone) {
this.photo = phone;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUserType() {
return userType;
}
public void setUserType(int userType) {
this.userType = userType;
}
public String getVillage() {
return village;
}
public void setVillage(String village) {
this.village = village;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobileAbbr() {
return mobileAbbr;
}
public void setMobileAbbr(String mobileAbbr) {
this.mobileAbbr = mobileAbbr;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
public static class ResourceInfo{
private String group_logo_path;
private String user_photo_path;
@Override
public String toString() {
return "ResourceInfo{" +
"group_logo_path='" + group_logo_path + '\'' +
", user_photo_path='" + user_photo_path + '\'' +
'}';
}
public String getGroup_logo_path() {
return group_logo_path;
}
public void setGroup_logo_path(String group_logo_path) {
this.group_logo_path = group_logo_path;
}
public String getUser_photo_path() {
return user_photo_path;
}
public void setUser_photo_path(String user_photo_path) {
this.user_photo_path = user_photo_path;
}
}
}
定义服务接口:
因为是登录的接口,我把他取名为LoginService.class
package com.example.administrator.retrofit.bean;
import java.util.List;
/**
* 登录的信息
*/
/**
* 登录接口
*/
public interface LoginService {
@POST("/app/user/login.htm")
Call<LoginInfo> getLoginInfoList(@Query("mobile") String mobile,
@Query("password") String password,
@Query("deviceID") String deviceID);
}
注解:
@GET 是get的请求方式
@Query 是接口查询的关键字
封装接口服务的包装类
/**
* 接口服务的包装类
*/
public class RetrofitWrapper {
private static RetrofitWrapper instance;
private Context mContext;
private Retrofit retrofit;
private RetrofitWrapper() {
retrofit = new Retrofit.Builder().baseUrl(Constant.BASE_URL).
addConverterFactory(GsonConverterFactory.create()).build();
}
public static RetrofitWrapper getInstance() {
if (instance == null) {
synchronized (RetrofitWrapper.class) {
instance = new RetrofitWrapper();
}
}
return instance;
}
public <T> T create(Class<T> service) {
return retrofit.create(service);
}
public class Constant {
public static final String BASE_URL = "http://xxx.xx.xx.xx";
}
}
如何调用:
在MainActivity中代码如下:
LoginService loginService = RetrofitWrapper.getInstance().create(LoginService.class);
Call<LoginInfo> call = loginService.getLoginInfoList(
"xxxx",
"xxxx", "000000000000000");
call.enqueue(new Callback<LoginInfo>() {
@Override
public void onResponse(Call<LoginInfo> call, Response<LoginInfo>
response) {
LoginInfo loginInfo=response.body();
tvShow.setText(loginInfo.toString());
}
@Override
public void onFailure(Call<LoginInfo> call, Throwable t) {
tvShow.setText(t.getMessage());
}
});
进一步解释
首先调用我们封装的网络库,创建retrofit和对应的接口服务
然后调用我们的请求接口方法;
发送请求,并回调处理结果,call.enqueue();