//使用xUtils框架需要有以下权限:网络请求、外部存储
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1.在APP的gradle里的dependencies声明一下
//引进xutils
compile 'org.xutils:xutils:3.3.40'
//引进fastjson
compile 'com.alibaba:fastjson:1.1.52.android'
在android里加入
//引进fastjson
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
productFlavors {
}
repositories {
flatDir {
dirs 'libs' //this way we can find the .aar file in libs folder
}
maven {
url "https://jitpack.io"
}
}
2.在Application的onCreate函数里初始化xUtils:
//初始化xUtils
x.Ext.init(this);
( 此处我新建了一个BaseApplication extends Application,要使用BaseApplication ,需要去AndroidMainfest.xml里为application添加 android:name=”.BaseApplication” 这样才能起作用)
3.准备工作结束,现在开始直接上代码(UserMode的使用必须在4、5步骤的前提下):
RequestParams requestParams = new RequestParams(Constant.LOGINURL);
requestParams.addBodyParameter("username", userName);
requestParams.addBodyParameter("password", password);
//UserMode为自己定义的数据类型,根据需要自行更改(改为String后,可在onSuccess里输出整个获取到的数据)
x.http().post(requestParams, new Callback.CommonCallback<UserMode>() {
@Override
public void onSuccess(UserMode userMode) {
tvShowInfor.setText("登陆成功" );
if (userMode == null) {
return;
}
UserMode.ObjBean user = userMode.getObj();
if (user != null) {
tvShowInfor.setText("登录名 "+userMode.getObj().getDengluming());
boolean b = CookieUtil.saveUserInfo(userMode);//把用户信息存入cookie
if (b) {
CookieUtil.setAuth(true);//标记已存入
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
finish();
}
} else {
if (!TextUtils.isEmpty(userMode.getMsg())) {
tvShowInfor.setText(userMode.getMsg());
}
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
tvShowInfor.setText("登陆失败,请重试");
ex.printStackTrace();
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
4.JsonRespondParse为自定义类,用于处理Json数据,功能自行决定
import com.alibaba.fastjson.JSON;
import org.xutils.common.util.ParameterizedTypeUtil;
import org.xutils.http.app.ResponseParser;
import org.xutils.http.request.UriRequest;
import java.lang.reflect.Type;
import java.util.List;
public class JsonRespondParse implements ResponseParser {
@Override
public void checkResponse(UriRequest request) throws Throwable {
}
@Override
public Object parse(Type resultType, Class<?> resultClass, String result) throws Throwable {
//判断整个数据是不是由一个List包裹起来的
if (resultClass == List.class) {
//将List解析成一个个小的对象(List的名字叫datas)
result = JSON.parseObject(result).getString("datas");
//解析成小的vo对象
return JSON.parseArray(result, (Class<?>) ParameterizedTypeUtil.getParameterizedType(resultType, List.class, 0));
} else { //如果不是用List组成的则直接将Json进行解析成vo实体类
return JSON.parseObject(result, resultClass);
}
}
}
5.新建一个类名为 UserMode,并利用json数据结构自动生成其余代码,完整代码如下:
import com.lotus.cookiedemo.helper.JsonRespondParse;
import org.xutils.http.annotation.HttpResponse;
import java.io.Serializable;
//JsonRespondParse为自定义类,并 implements ResponseParser
//一定要写上这一句话,用于自动按规则解析Json数据
@HttpResponse(parser = JsonRespondParse.class)
public class UserMode implements Serializable {
private boolean success;
private String msg;
private ObjBean obj;
private Object list;
private String js;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public ObjBean getObj() {
return obj;
}
public void setObj(ObjBean obj) {
this.obj = obj;
}
public Object getList() {
return list;
}
public void setList(Object list) {
this.list = list;
}
public String getJs() {
return js;
}
public void setJs(String js) {
this.js = js;
}
public static class ObjBean implements Serializable{
private int sysyonghuid;
private String dengluming;
private String mima;
private String xingming;
private String xingbie;
private String dianhua;
private String shengri;
private String shangciriqi;
private String shangciip;
private String bumen;
private String juese;
private String touxiang;
private String token;
public int getSysyonghuid() {
return sysyonghuid;
}
public void setSysyonghuid(int sysyonghuid) {
this.sysyonghuid = sysyonghuid;
}
public String getDengluming() {
return dengluming;
}
public void setDengluming(String dengluming) {
this.dengluming = dengluming;
}
public String getMima() {
return mima;
}
public void setMima(String mima) {
this.mima = mima;
}
public String getXingming() {
return xingming;
}
public void setXingming(String xingming) {
this.xingming = xingming;
}
public String getXingbie() {
return xingbie;
}
public void setXingbie(String xingbie) {
this.xingbie = xingbie;
}
public String getDianhua() {
return dianhua;
}
public void setDianhua(String dianhua) {
this.dianhua = dianhua;
}
public String getShengri() {
return shengri;
}
public void setShengri(String shengri) {
this.shengri = shengri;
}
public String getShangciriqi() {
return shangciriqi;
}
public void setShangciriqi(String shangciriqi) {
this.shangciriqi = shangciriqi;
}
public String getShangciip() {
return shangciip;
}
public void setShangciip(String shangciip) {
this.shangciip = shangciip;
}
public String getBumen() {
return bumen;
}
public void setBumen(String bumen) {
this.bumen = bumen;
}
public String getJuese() {
return juese;
}
public void setJuese(String juese) {
this.juese = juese;
}
public String getTouxiang() {
return touxiang;
}
public void setTouxiang(String touxiang) {
this.touxiang = touxiang;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
}