先在build.gradle中依赖
compile 'org.xutils:xutils:3.5.0'
他需要初始化所以为了解决频繁的初始化,我新建了一个
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
}
}
在清单中添加name声明
然后再去Activity或fragment中使用
Activity中使用
package com.example.administrator.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
@ContentView(R.layout.activity_main)//设置布局
public class MainActivity extends AppCompatActivity{
@ViewInject(R.id.button)//相当于findViewByid
Button bt1;
@ViewInject(R.id.textView)
TextView setText;
Callback.Cancelable cancelable;//回掉
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
//让Activity识别注解
x.view().inject(this);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "我喜欢你", Toast.LENGTH_SHORT).show();
//get
String url = "http://v.juhe.cn/toutiao/index";
RequestParams params = new RequestParams(url);
params.addQueryStringParameter("type","");
params.addQueryStringParameter("key","2ca3a5b1cb6edf55250bff550ac34325");
//NetWorkonMainThread
cancelable = x.http().get(params ,new Callback.CommonCallback(){
@Override
public void onSuccess(String result) {
Log.d("data",result);
Log.d("data","1");
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
cancelable.cancel();
}
@Override
public void onCancelled(CancelledException cex) {
Log.d("data","3");
}
@Override
public void onFinished() {
setText.setText("获取结束");
Log.d("data","2");
}
});
}
});
}
//访问修饰符必须是私有的
//绑定短按监听 type可以省略
// @Event(type = View.OnClickListener.class,value = R.id.button2)
@Event(value = R.id.button2)
private void MyonClickListener(View v){
Toast.makeText(this, "你看看我", Toast.LENGTH_SHORT).show();
}
//长按监听 type不可省略
@Event(type = View.OnLongClickListener.class,value = R.id.button2)
private boolean MyonLongClickListener(View v){
bt1.setText("你看看我,张开眼");
return true;
}
}
fragment中使用
@ContentView(R.layout.fragment_http)
public class HttpFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) {
return x.view().inject(this, inflater, container);
}
xutils网络请求x.http().post或者x.http().get
get方法
RequestParams params = new RequestParams(url);
cancelable = x.http().get(params ,new Callback.CommonCallback(){
@Override
public void onSuccess(String result) {//成功的
setText.setText(result);
Log.d("data","1");
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
cancelable.cancel();
}
@Override
public void onCancelled(CancelledException cex) {
Log.d("data","3");当取消时
}
@Override
public void onFinished() {//完成的
//先成功后完成
Log.d("data","2");
}
});
post
String url1 = "http://apicloud.mob.com/v1/weather/query";
RequestParams params1 = new RequestParams(url1);
params1.addQueryStringParameter("key", "26b2b13b4b440");
params1.addQueryStringParameter("city", "通州");
params1.addQueryStringParameter("province", "北京");
Callback.Cancelable cancelable1 = x.http().post(params1,
new Callback.CommonCallback() {
@Override
public void onSuccess(String result) {
setText.setText(result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
xutils图片x.image()
注意设置不了GIF
String url2 = "http://p0.so.qhmsg.com/bdr/200_200_/t015576ba03bd0daf6f.jpg";
RequestParams params2 = new RequestParams(url2);
final File file = new File(
Environment.getExternalStorageDirectory() + "/a123.jpg");
// file.mkdir();//创建文件夹
// file.mkdirs();//创建多级文件夹
params2.setSaveFilePath(file.getAbsolutePath());//设置下载文件时文件保存的路径和文件名
params2.setAutoRename(true);//设置是否根据头信息自动命名文件
Callback.Cancelable can = x.http().post(params2,
new Callback.ProgressCallback() {
@Override
public void onSuccess(File result) {
//下载成功后读取文件
imageView.setImageBitmap(
BitmapFactory.decodeFile(file.getAbsolutePath()));
Toast.makeText(MainActivity.this, "下载成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Toast.makeText(MainActivity.this, ex.toString(), Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
@Override
public void onWaiting() {
}
@Override
public void onStarted() {
Toast.makeText(MainActivity.this, "开始下载", Toast.LENGTH_SHORT).show();
}
@Override
public void onLoading(long total, long current, boolean isDownloading) {
progressBar.setMax((int) total);
progressBar.setProgress((int) current);
}
});
2x.image().bind()方法有时候会把图片的大小放大显示不全等问题
//自定义动画
final AlphaAnimation alphaAnimation = new AlphaAnimation(0f,1.0f);
alphaAnimation.setDuration(1500);
alphaAnimation.setFillAfter(true);
ImageOptions options = new ImageOptions.Builder()
// .setFadeIn(true)//设置加载图片的动画渐变效果
// .setCircular(true)//设置圆形
.setAnimation(alphaAnimation)
.setFailureDrawableId(R.mipmap.ic_launcher)//设置故障提图片
.setLoadingDrawableId(R.mipmap.ic_launcher_round)//设置加载图片
.setRadius(20)//设置半径
.setUseMemCache(false)//设置程序缓存
.build();
x.image().bind(imageView,
"http://img3.duitang.com/uploads/item/201609/11/20160911132450_nfCxK.thumb.700_0.jpeg",
// "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533813535460&di=731953ed39ef9eb3073b0c0357e26f32&imgtype=0&src=http%3A%2F%2Fi1.mopimg.cn%2Fimg%2Fdzh%2F2015-01%2F468%2F2015012122374323.gif440.gif",
options
, new Callback.CommonCallback() {
@Override
public void onSuccess(Drawable result) {
imageView.setImageDrawable(result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
3,用x.image().loadDrawable加载图片
Callback.Cancelable cancelable2 = x.image().loadDrawable(
"http://img3.duitang.com/uploads/item/201609/11/20160911132450_nfCxK.thumb.700_0.jpeg",
options, new Callback.CommonCallback() {
@Override
public void onSuccess(Drawable result) {
imageView.setImageDrawable(result);
imageView.startAnimation(alphaAnimation);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
}
);
4,x.image().loadFile加载文件方式加载图片
//注意用时看一下后缀名
Callback.Cancelable cancelable3 = x.image().loadFile(
"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1533814281311&di=6be8244223b0c242d60b561128c77b98&imgtype=0&src=http%3A%2F%2Fpic25.nipic.com%2F20121203%2F668573_115703380139_2.jpg",
options,
new Callback.CacheCallback() {
@Override
public void onSuccess(File result) {
Log.e("tag", "onSuccess: "+Environment.getExternalStorageDirectory().getPath());
// 可以把网络的图片下载到SDcard中
try {
//把文件类型的数据转换成字节数据
FileInputStream fi = new FileInputStream(result);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
byte buffer[] = new byte[512];
int length = -1;
while( (length = fi.read(buffer)) != -1 ){
bs.write(buffer,0,length);
bs.flush();
}
File file1 = new File(Environment.getExternalStorageDirectory()
+"/my1710.jpg");
FileOutputStream fs = new FileOutputStream(file1);
fs.write(bs.toByteArray(),0,bs.toByteArray().length);
fs.flush();
fi.close();
bs.close();
fs.close();
} catch (Exception e) {
e.printStackTrace();
}
imageView.setImageBitmap(BitmapFactory.decodeFile(result.getAbsolutePath()));
Toast.makeText(MainActivity.this, "onSuccess", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
@Override
public boolean onCache(File result) {//读取缓存
Toast.makeText(MainActivity.this, "onCache", Toast.LENGTH_SHORT).show();
imageView.setImageBitmap(BitmapFactory.decodeFile(result.getAbsolutePath()));
return false;
}
}
);