使用登录接口为例,baseURL = “http://fpush.sayimo.cn/schoolapi/v1.0/”
配置:
Constant.java
...
public static final String APP_SERVICE_ADDRESS = "http://fpush.sayimo.cn/schoolapi/v1.0/";
public static final String SERVICE_INTERFACE_VERSION = "1.0.0";//服务器接口版本号
...
请求实体类:
/**
* description :
* project name:NumberSchoolS
* author : Vincent
* creation date: 2017/1/5 9:02
*
* @version 1.0
*/
public class LoginEntity implements Serializable {
/**
* current_version : 1.0
* client_token : AjD0BlcUEQMMJeN32G24VFRMvW7lRmlWkZSsuXUfl6xk
* client_type : android
* signature : MTU1MjU1MTE0NDQ6dHJyZmZm
*/
private String current_version;
private String client_token;
private String client_type;
private String signature;
public String getCurrent_version() {
return current_version;
}
public void setCurrent_version(String current_version) {
this.current_version = current_version;
}
public String getClient_token() {
return client_token;
}
public void setClient_token(String client_token) {
this.client_token = client_token;
}
public String getClient_type() {
return client_type;
}
public void setClient_type(String client_type) {
this.client_type = client_type;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
}
登录结果实体类:
/**
* description :
* project name:NumberSchoolS
* author : Vincent
* creation date: 2017/1/5 9:42
*
* @version 1.0
*/
public class LoginResultEntity implements Serializable {
/**
* status : SUCCESS
* data : [{"accesstoken":"d8ca0ff9d4295dc4c8c4d025267407fca70fa00c3847f6a7d12a4f3b","role":1}]
*/
private String status;
private String msg;
private List<DataBean> data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* accesstoken : d8ca0ff9d4295dc4c8c4d025267407fca70fa00c3847f6a7d12a4f3b
* role : 1
*/
private String accesstoken;
private int role;
public String getAccesstoken() {
return accesstoken;
}
public void setAccesstoken(String accesstoken) {
this.accesstoken = accesstoken;
}
public int getRole() {
return role;
}
public void setRole(int role) {
this.role = role;
}
}
}
//PostRoute post请求设置
import com.ourymy.school.entity.result.LoginResultEntity;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;
/**
* description :
* project name:NumberSchoolS
* author : Vincent
* creation date: 2017/1/5 8:58
*
* @version 1.0
*/
public interface PostRoute {
@Headers({"Content-type:application/json;charset=utf-8","Accept:application/json"})//需要添加touch
@POST("public/user/login/")
Call<LoginResultEntity> login(@Body RequestBody route);
}
//组建请求的json数据:
LoginEntity login = new LoginEntity();
login.setCurrent_version(Constant.SERVICE_INTERFACE_VERSION);//这个版本号是接口的版本
login.setClient_token(App.getShared().getString("deviceToken"));
login.setClient_type("android");
login.setSignature(Base64Utils.string2Base64(phone + ":" + password));
//转为Json String类型
Gson gson = new Gson();
String route = gson.toJson(login);
ViseLog.e("postjson " + route);
//具体的网络请求
//网络请求..
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constant.APP_SERVICE_ADDRESS)
.addConverterFactory(GsonConverterFactory.create())
.build();
PostRoute postRoute = retrofit.create(PostRoute.class);
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), route);
Call<LoginResultEntity> call = postRoute.login(body);
call.enqueue(new Callback<LoginResultEntity>() {
@Override
public void onResponse(Call<LoginResultEntity> call, Response<LoginResultEntity> response) {
ViseLog.e(".....................");
LoginResultEntity result = response.body();
switch (result.getStatus()){
case "FAIL":
view.errorMsg("接口版本不存在");
break;
case "SUCCESS":
view.errorMsg("登录成功");
view.loginSuccess();
App.getShared().putString("accesstoken",result.getData().get(0).getAccesstoken());
App.getShared().putInt("role",result.getData().get(1).getRole());
break;
}
}
@Override
public void onFailure(Call<LoginResultEntity> call, Throwable t) {
ViseLog.e(".....................55");
}
});
//整个登录的网络请求:
@Override
public void login(Context context, String phone, String password) {
if (TextUtils.isEmpty(phone)) {
view.errorMsg("账户不能为空");
return;
}
if (!StringUtil.isPhoneNumber(phone)) {
view.errorMsg("这不是一个有效的手机号码");
return;
}
if (TextUtils.isEmpty(password)) {
view.errorMsg("密码不能为空");
return;
}
if (password.length() > 16 || password.length() < 6) {
view.errorMsg("密码长度为6~16位数字或字母");
return;
}
LoginEntity login = new LoginEntity();
login.setCurrent_version(Constant.SERVICE_INTERFACE_VERSION);//这个版本号是接口的版本
login.setClient_token(App.getShared().getString("deviceToken"));
login.setClient_type("android");
login.setSignature(Base64Utils.string2Base64(phone + ":" + password));
Gson gson = new Gson();
String route = gson.toJson(login);
ViseLog.e("postjson " + route);
//网络请求..
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constant.APP_SERVICE_ADDRESS)
.addConverterFactory(GsonConverterFactory.create())
.build();
PostRoute postRoute = retrofit.create(PostRoute.class);
RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), route);
Call<LoginResultEntity> call = postRoute.login(body);
call.enqueue(new Callback<LoginResultEntity>() {
@Override
public void onResponse(Call<LoginResultEntity> call, Response<LoginResultEntity> response) {
ViseLog.e(".....................");
LoginResultEntity result = response.body();
switch (result.getStatus()){
case "FAIL":
view.errorMsg("接口版本不存在");
break;
case "SUCCESS":
view.errorMsg("登录成功");
view.loginSuccess(); App.getShared().putString("accesstoken",result.getData().get(0).getAccesstoken()); App.getShared().putInt("role",result.getData().get(1).getRole());
break;
}
}
@Override
public void onFailure(Call<LoginResultEntity> call, Throwable t) {
ViseLog.e(".....................55");
}
});
}