Android Volley框架使用(三)

时间:2022-06-30 19:46:48

Android Volley框架使用(一)
Android Volley框架使用(二)

这次主要是自定义请求队列

主要代码

package com.example.hfs.volleycustomrequestqueuedemo;

import android.content.Context;
import android.os.Environment;

import com.android.volley.Cache;
import com.android.volley.Network;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.HttpStack;
import com.android.volley.toolbox.HurlStack;

import java.io.File;

/**
* Created by HFS on 2016/4/26.
*/


//这个类对外提供RequestQueue请求队列
public class MyVolley {
// 声明一个全局的RequestQueue
public static RequestQueue mRequestQueue;

// 采用单例模式
// 1.私有构造方法 只能自己调用自己

private MyVolley(Context context) {
// 在私有的构造方法中进行赋值
if (mRequestQueue == null) {
String sdPath = null;

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
sdPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "myVolleyCache";
} else {
sdPath = context.getCacheDir().getAbsolutePath() + File.separator + "myVolleyCache";
}

File rootDirectory = new File(sdPath);

if (!rootDirectory.exists()) {
// 文件夹不存在,创建
rootDirectory.mkdirs();
}
Cache cache = new DiskBasedCache(rootDirectory, 1024 * 1024 * 200);

// 如果使用HurlStack,RequestQueue联网请求方式采用HttpUrlConnection
// HttpClientStack 联网请求方式采用HttpClient
HttpStack httpStack = new HurlStack();
Network network = new BasicNetwork(httpStack);
mRequestQueue = new RequestQueue(cache, network, 6);
}

}

// 2.提供公共的访问方法
public static RequestQueue newInstance(Context context) {

//调用私有的构造方法
new MyVolley(context);
return mRequestQueue;
}

}
package com.example.hfs.volleycustomrequestqueuedemo;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;

public class MainActivity extends AppCompatActivity {
private ImageView mImageView;
private RequestQueue mRequestQueue;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1.创建RequestQueue
// 这是自定义的RequestQueue缓存在SD卡
mRequestQueue = MyVolley.newInstance(this);
// 如果是自定义的RequestQueue 必须调用start方法
mRequestQueue.start();

initView();
}

private void initView() {
mImageView = (ImageView) findViewById(R.id.showImg);
}

public void btnCustomRequest(View view) {
// 2创建一个请求
String url = "http://img.my.csdn.net/uploads/201407/26/1406382879_2553.jpg";
ImageRequest request = new ImageRequest(url,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
if (response != null) {
mImageView.setImageBitmap(response);
} else {
mImageView.setImageResource(R.mipmap.ic_launcher);
}
}
},
150,
300,
ImageView.ScaleType.FIT_XY,
Bitmap.Config.ARGB_8888,
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "请求图片失败", Toast.LENGTH_SHORT).show();
}
});

// 3.添加到请求队列中
mRequestQueue.add(request);
}
}