Xutils

简介: Xutils3网络连接代码

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">

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();

}

}

目录
相关文章
|
缓存 API 调度
Android OkHttp+Retrofit+Rxjava+Hilt实现网络请求框架
🔥 介绍 本文通过OkHttp+Retrofit+Rxjava+Hilt实现一个网络请求框。
766 0
Android OkHttp+Retrofit+Rxjava+Hilt实现网络请求框架
|
Android开发
Android Retrofit,Gson,Okhttp混淆
Android Retrofit,Gson,Okhttp混淆
518 0
关于volley的使用
https://blog.csdn.net/qwm8777411/article/details/45770979
713 0
|
JSON Java Android开发
|
JSON Android开发 数据格式
Android Json处理框架
1、Android 中的Json解析工具fastjson 、序列化、反序列化   2、Android Gson的使用总结   3、Android-JSONTool 一个简易的Json框架类,小到只有一个类     有时为了简化代码的大小,尽可能的压缩apk的大小。
962 0
|
SQL 缓存 Android开发
android xutils 基本使用 方法
 下载地址:https://github.com/wyouflf/xUtils     实战 QQ demo源码(本例中有该应用) 服务器端下载:http://download.csdn.
980 0
|
SQL 缓存 Java
android--xUtils开发框架
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/51955116 xUtils简介 xUtils 包含了很多实用的android工具。
894 0