先在build.gradle中依賴
compile 'org.xutils:xutils:3.5.0'
他需要初始化所以為了解決頻繁的初始化,我新建了一個(gè)
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
}
}
在清單中添加name聲明
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:name=".MyApplication"
然后再去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)//設(shè)置布局
public class MainActivity extends AppCompatActivity{
@ViewInject(R.id.button)//相當(dāng)于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識(shí)別注解
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<String>(){
@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("獲取結(jié)束");
Log.d("data","2");
}
});
}
});
}
//訪問(wèn)修飾符必須是私有的
//綁定短按監(jiān)聽(tīng) 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();
}
//長(zhǎng)按監(jiān)聽(tīng) 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網(wǎng)絡(luò)請(qǐng)求x.http().post或者x.http().get
get方法
RequestParams params = new RequestParams(url);
cancelable = x.http().get(params ,new Callback.CommonCallback<String>(){
@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");當(dāng)取消時(shí)
}
@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<String>() {
@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()
注意設(shè)置不了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();//創(chuàng)建文件夾
// file.mkdirs();//創(chuàng)建多級(jí)文件夾
params2.setSaveFilePath(file.getAbsolutePath());//設(shè)置下載文件時(shí)文件保存的路徑和文件名
params2.setAutoRename(true);//設(shè)置是否根據(jù)頭信息自動(dòng)命名文件
Callback.Cancelable can = x.http().post(params2,
new Callback.ProgressCallback<File>() {
@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()方法有時(shí)候會(huì)把圖片的大小放大顯示不全等問(wèn)題
//自定義動(dòng)畫
final AlphaAnimation alphaAnimation = new AlphaAnimation(0f,1.0f);
alphaAnimation.setDuration(1500);
alphaAnimation.setFillAfter(true);
ImageOptions options = new ImageOptions.Builder()
// .setFadeIn(true)//設(shè)置加載圖片的動(dòng)畫漸變效果
// .setCircular(true)//設(shè)置圓形
.setAnimation(alphaAnimation)
.setFailureDrawableId(R.mipmap.ic_launcher)//設(shè)置故障提圖片
.setLoadingDrawableId(R.mipmap.ic_launcher_round)//設(shè)置加載圖片
.setRadius(20)//設(shè)置半徑
.setUseMemCache(false)//設(shè)置程序緩存
.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<Drawable>() {
@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<Drawable>() {
@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加載文件方式加載圖片
//注意用時(shí)看一下后綴名
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<File>() {
@Override
public void onSuccess(File result) {
Log.e("tag", "onSuccess: "+Environment.getExternalStorageDirectory().getPath());
// 可以把網(wǎng)絡(luò)的圖片下載到SDcard中
try {
//把文件類型的數(shù)據(jù)轉(zhuǎn)換成字節(jié)數(shù)據(jù)
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;
}
}
);