1在build.gradle中引包
compile'org.xutils:xutils:3.3.36'
2创建MyApplication初始化xutils
public class MyApplication extends Application {
@Override
public voidonCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(BuildConfig.DEBUG);// 是否输出debug日志, 开启debug会影响性能.
}
3在AndroiManifast.xml中声明权限并引入MyApplication
<uses-permissionandroid:name="android.permission.INTERNET"/>
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:name=".base.MyApplication"
4 xml页面
<?xml version="1.0" encoding="utf-8"?>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hyad.xinhualianshe.XiazaiActivity"
android:orientation="vertical"
android:weightSum="1">
android:id="@+id/get"
android:text="GET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/httptextView" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/httpimageView"
android:layout_gravity="center_horizontal"
android:layout_weight="1.10" />
5Activity生成http连接方法
@ContentView(R.layout.activity_xiazai)
public classhttptestextendsAppCompatActivity {
// private Context context;
@ViewInject(R.id.get)
privateButtonGET;
@ViewInject(R.id.post)
privateButtonPOST;
@ViewInject(R.id.other)
privateButtonOTHER;
@ViewInject(R.id.download)
privateButtonDOWNLOAD;
@ViewInject(R.id.cache)
privateButtonCACHE;
@ViewInject(R.id.httptextView)
privateTextViewTX;
@ViewInject(R.id.httpimageView)
privateImageViewimageView;
ImageOptionsimageOptions;
@Override
protected voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//注入view和事件
x.view().inject(this);
// context=this;
imageOptions=newImageOptions.Builder()
.setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(120))
.setRadius(DensityUtil.dip2px(5))
// 如果ImageView的大小不是定义为wrap_content, 不要crop.
.setCrop(true)
// 加载中或错误图片的ScaleType
//.setPlaceholderScaleType(ImageView.ScaleType.MATRIX)
.setImageScaleType(ImageView.ScaleType.CENTER_CROP)
//设置加载过程中的图片
.setLoadingDrawableId(R.mipmap.ic_launcher)
//设置加载失败后的图片
.setFailureDrawableId(R.mipmap.ic_launcher)
//设置使用缓存
.setUseMemCache(true)
//设置支持gif
.setIgnoreGif(false)
//设置显示圆形图片
// .setCircular(false)
.build();
}
String url ="http://www.baidu.com/";
@Event( R.id.get)
private voidget(View v){
finalProgressDialog progressDialog =newProgressDialog(this);
progressDialog.setMessage("正在下载");
RequestParams params =newRequestParams(url);
params.addQueryStringParameter("username","11000");
params.addQueryStringParameter("password","888888");
Callback.Cancelable cancelable = x.http().get(params,newCallback.CommonCallback() {
@Override
public voidonSuccess(String result) {
Log.i("get_onSuccess","result"+ result);
}
@Override
public voidonError(Throwable ex,booleanisOnCallback) {
}
@Override
public voidonCancelled(CancelledException cex) {
}
@Override
public voidonFinished() {
//请求不管成功还是失败都会调用
progressDialog.cancel();
}
});
cancelable.cancel();
}
@Event(value={R.id.httpimageView})
private voidloadImage(View view){
x.image().bind(imageView,"http://img2.3lian.com/2014/f2/164/d/17.jpg",imageOptions);
}
@Event(R.id.post)
private voidpost(View view){
RequestParams params =newRequestParams(url);
params.addBodyParameter("username","12323");
//根据当前请求方式添加参数位置
params.addParameter("password","2313");
// params.addHeader("head","xs");
x.http().post(params,newCallback.CommonCallback() {
@Override
public voidonSuccess(String result) {
Log.i("get_onSuccess","result"+ result);
TX.setText(result);
}
@Override
public voidonError(Throwable ex,booleanisOnCallback) {
}
@Override
public voidonCancelled(CancelledException cex) {
}
@Override
public voidonFinished() {
}
});
}
@Event(R.id.other)
private voidother(View view){
RequestParams params =newRequestParams(url);
params.addBodyParameter("usrname","34234");
x.http().request(HttpMethod.PUT, params,newCallback.CommonCallback() {
@Override
public voidonSuccess(String result) {
}
@Override
public voidonError(Throwable ex,booleanisOnCallback) {
}
@Override
public voidonCancelled(CancelledException cex) {
}
@Override
public voidonFinished() {
}
});
}
@Event(R.id.upload)
private voidupload(View view){
String path ="//";
RequestParams params =newRequestParams(url);
params.addBodyParameter("file",newFile(path));
x.http().post(params,newCallback.CommonCallback() {
@Override
public voidonSuccess(String result) {
}
@Override
public voidonError(Throwable ex,booleanisOnCallback) {
}
@Override
public voidonCancelled(CancelledException cex) {
}
@Override
public voidonFinished() {
}
});
}
@Event(R.id.download)
private voiddownload(View view){
url="";
RequestParams params =newRequestParams(url);
Callback.Cancelable post = x.http().post(params,newCallback.ProgressCallback() {
@Override
public voidonSuccess(File result) {
}
@Override
public voidonError(Throwable ex,booleanisOnCallback) {
}
@Override
public voidonCancelled(CancelledException cex) {
}
@Override
public voidonFinished() {
}
@Override
public voidonWaiting() {
}
@Override
public voidonStarted() {
}
@Override
public voidonLoading(longtotal,longcurrent,booleanisDownloading) {
//文件下载时回调的方法
Log.i("xxxxxxxxxxxxx","current<<"+current+"total<<"+total);
}
});
}
@Event(R.id.cache)
private voidcache(View view){
RequestParams params =newRequestParams(url);
params.setCacheMaxAge(1000*60);
Callback.Cancelable cancelable = x.http().get(params,newCallback.CacheCallback() {
@Override
public booleanonCache(String result) {
//false 不相信本地缓存
//相信本地缓存
Log.i("sdadasd","cache<<"+result);
return false;
}
@Override
public voidonSuccess(String result) {
Log.i("get_onSuccess","result"+ result);
}
@Override
public voidonError(Throwable ex,booleanisOnCallback) {
}
@Override
public voidonCancelled(CancelledException cex) {
}
@Override
public voidonFinished() {
//请求不管成功还是失败都会调用
}
});
cancelable.cancel();
}
}