把jar包导入到项目中,进行相关设置
1、权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2、在自定义的Application中进行初始化
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
x.Ext.init(this);// Xutils初始化
3、在Activity中使用( 以post请求为例,get请求为x.http().get() )
RequestParams parms = new RequestParams(url);//请求的url
//有参数的时候,添加参数
parms.addBodyParameter("key", "name");
x.http().post(parms, new CommonCallback<String>() {
@Override
public void onCancelled(CancelledException arg0) {
// 请求取消
}
@Override
public void onError(Throwable arg0, boolean arg1) {
System.out.println(arg0.getMessage());//失败
}
@Override
public void onFinished() {
//请求完成的时候执行
}
@Override
public void onSuccess(String arg0) {
System.out.println(arg0);
//在请求成功之后,获取cookies信息
DbCookieStore instance = DbCookieStore.INSTANCE;
List<HttpCookie> cookies = instance.getCookies();
for (HttpCookie cookie : cookies) {
String name = cookie.getName();
String value = cookie.getValue();
}
}
});