Android Volley框架使用(三)
来源:互联网 发布:软件外包介绍网站 编辑:程序博客网 时间:2024/06/05 09:50
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); }}
1 0
- Android Volley框架使用(三)
- Android Volley框架使用(三)
- Android Volley框架的使用(三)
- Android Volley框架的使用(三)
- Android Volley框架使用
- Android Volley框架使用详解
- Android Volley框架使用详解
- Android Volley框架使用详解
- Android Volley 框架的使用
- Android Volley框架使用详解
- Android Volley 框架的使用
- Android Volley框架的使用
- Android Volley框架使用详解
- Android Volley框架使用(一)
- Android Volley框架使用(二)
- Android Volley框架使用(一)
- Android Volley框架使用(二)
- Android Volley 框架的使用
- Servlet 的生命周期 和 工作原理
- 204. Count Primes
- 2003 - Can connect to MySQL server on localhost (10038)mysql 读取描述文件失败 错误代码:2【亲测可用】
- Java构造器
- 微信公众平台开发[1] —— 获取用户Openid及个人信息
- Android Volley框架使用(三)
- Windows注册表操作
- 关押罪犯【C++】
- mysql的备份与恢复
- android之timer和timertask是否运行在主线程中解答
- 网络信息安全学习平台---解密关第9题
- Android中的画廊(Gallery)
- 折纸的艺术
- Java 中InputStream与Reader的区别