不会写服务端代码的伙伴可以去小白接口注册自己的账户https://www.okayapi.com/
1.导入依赖
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.3'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
2.添加互联网权限
3.写登录实体类
public class token {
/**
* ret : 200
* data : {"err_code":0,"err_msg":"","uuid":"55CBA451AEB4D5569DE3D3496C72BA0C","token":"F58D72C7485FD4586505A6F926CD1D2592FB61F3ACACEC9E9A4ADE74D033DE51"}
* msg : 当前请求接口:App.User.Login
*/
private int ret;
private DataBean data;
private String msg;
public int getRet() {
return ret;
}
public void setRet(int ret) {
this.ret = ret;
}
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public static class DataBean {
/**
* err_code : 0
* err_msg :
* uuid : 55CBA451AEB4D5569DE3D3496C72BA0C
* token : F58D72C7485FD4586505A6F926CD1D2592FB61F3ACACEC9E9A4ADE74D033DE51
*/
private int err_code;
private String err_msg;
private String uuid;
private String token;
public int getErr_code() {
return err_code;
}
public void setErr_code(int err_code) {
this.err_code = err_code;
}
public String getErr_msg() {
return err_msg;
}
public void setErr_msg(String err_msg) {
this.err_msg = err_msg;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
}
3.写用户实体类
import com.google.gson.Gson;
public class User {
/**
* ret : 200
* data : {"err_code":0,"err_msg":"","info":{"uuid":"55CBA451AEB4D5569DE3D3496C72BA0C","username":"terong","role":"user","rolename":"普通用户","register_time":"2018-10-11 10:40:35","register_ip":"47.92.213.241","ext_info":{}}}
* msg : 当前请求接口:App.User.Profile
*/
private int ret;
private DataBean data;
private String msg;
// public User(int ret, DataBean data, String msg) {
// this.ret = ret;
// this.data = data;
// this.msg = msg;
// }
//
public User(int ret, String msg) {
this.ret = ret;
this.msg = msg;
}
// public User(){
//
// }
public int getRet() {
return ret;
}
public void setRet(int ret) {
this.ret = ret;
}
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public static class DataBean {
/**
* err_code : 0
* err_msg :
* info : {"uuid":"55CBA451AEB4D5569DE3D3496C72BA0C","username":"terong","role":"user","rolename":"普通用户","register_time":"2018-10-11 10:40:35","register_ip":"47.92.213.241","ext_info":{}}
*/
private int err_code;
private String err_msg;
private InfoBean info;
public int getErr_code() {
return err_code;
}
public void setErr_code(int err_code) {
this.err_code = err_code;
}
public String getErr_msg() {
return err_msg;
}
public void setErr_msg(String err_msg) {
this.err_msg = err_msg;
}
public InfoBean getInfo() {
return info;
}
public void setInfo(InfoBean info) {
this.info = info;
}
public static class InfoBean {
/**
* uuid : 55CBA451AEB4D5569DE3D3496C72BA0C
* username : terong
* role : user
* rolename : 普通用户
* register_time : 2018-10-11 10:40:35
* register_ip : 47.92.213.241
* ext_info : {}
*/
private String uuid;
private String username;
private String role;
private String rolename;
private String register_time;
private String register_ip;
private ExtInfoBean ext_info;
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
public String getRegister_time() {
return register_time;
}
public void setRegister_time(String register_time) {
this.register_time = register_time;
}
public String getRegister_ip() {
return register_ip;
}
public void setRegister_ip(String register_ip) {
this.register_ip = register_ip;
}
public ExtInfoBean getExt_info() {
return ext_info;
}
public void setExt_info(ExtInfoBean ext_info) {
this.ext_info = ext_info;
}
public static class ExtInfoBean {
}
}
}
@Override
public String toString() {
return new Gson().toJson(this);
}
}
4.写Api接口
import java.util.Map;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;
public interface Api {
//获取用户信息
@GET("?s=App.User.Profile")
Call getUserInfowithMap(@QueryMap Map parms);
//登录
@POST("?s=App.User.Login")
Call userLogin1(@QueryMap Map parms);
//登录
@POST("?s=App.User.Login")
Call userLogin(@QueryMap Map parms);
}
5.
public class MapActivity extends AppCompatActivity {
Button btn;
Api api;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
btn = findViewById(R.id.btn);
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://hn2.api.okayapi.com/").addConverterFactory(GsonConverterFactory.create()).build();
api = retrofit.create(Api.class);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//1.登录 2.获取用户信息
final HashMap hashMap = new HashMap();
hashMap.put("username","terong");
hashMap.put("app_key","6E2453CFEDD692B7D380EA68DE7B5B84");
hashMap.put("password","c4b0f02ab9b5048f947d3c944e6ea3b7");
io.reactivex.Observable.just(hashMap).flatMap(new Function, ObservableSource>() {
@Override
public ObservableSource apply(HashMap stringStringHashMap) throws Exception {
token t = api.userLogin1(hashMap).execute().body();
System.out.println("-------------"+t.getData().getToken());
return io.reactivex.Observable.just(t);
}
}).flatMap(new Function>() {
@Override
public ObservableSource apply(token token) throws Exception {
HashMap hashMap1 = new HashMap();
hashMap1.put("app_key", "6E2453CFEDD692B7D380EA68DE7B5B84");
hashMap1.put("token", token.getData().getToken());
hashMap1.put("uuid", "55CBA451AEB4D5569DE3D3496C72BA0C");
hashMap1.put("username", "terong");
hashMap1.put("password", "c4b0f02ab9b5048f947d3c944e6ea3b72131");
User user = api.getUserInfowithMap(hashMap1).execute().body();
return io.reactivex.Observable.just(user);
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer() {
@Override
public void accept(User user) throws Exception {
System.out.println("---------------"+user.getData().getInfo().getRolename());
}
});
}
});
}
}
这就ok了 , 没看懂的留言