本文主要是介绍android框架Volley学习demo,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
(1)ImageLoader获取图片
public class HomeActivity extends Activity
{private ImageLoader mImageLoader;private RequestQueue requestQueue;private SWAdapter listSwAdapter;private void initValue(){this.requestQueue = Volley.newRequestQueue(this);this.mImageLoader = new ImageLoader(requestQueue,new ImageBitmapCache());this.mImageLoader.get("http://ar.ephhk.com/child/life/upload/apkicon/ic_ds.png", ImageLoader.getImageListener(this.mImageView, R.drawable.ic_launcher, R.drawable.ic_launcher));}
/*** get获取数据*/private void getJsonData(){String url = "http://ar.ephhk.com/child/life/index.php?r=api/GetCreativeSw";JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {@Overridepublic void onResponse(JSONObject jsonObject) {}},errorListener);requestQueue.add(jsonObjectRequest);}/*** post获取数据*/private void postJsonData(int categoryId){/*** 判断网络是否可用*/String url = "http://localhost/trialapk/index.php?r=api/GetSWList";HashMap<String,String> params = new HashMap<String,String>();params.put("pagesize",pagesize + "");params.put("pageindex",pageIndex + "");params.put("category_id",categoryId + "");params.put("pagecount",pagecount + "");JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,url, new JSONObject(params),new Response.Listener<JSONObject>() {@Overridepublic void onResponse(JSONObject jsonObject) {}},errorListener);}/*** 請求錯誤事件監聽*/private Response.ErrorListener errorListener= new Response.ErrorListener(){@Overridepublic void onErrorResponse(VolleyError error) {if (error instanceof NetworkError) {Toast.makeText(HomeActivity.this, "网路错误",Toast.LENGTH_SHORT);}else if (error instanceof ClientError) { Toast.makeText(HomeActivity.this, "客户端错误",Toast.LENGTH_SHORT);} else if (error instanceof ServerError) { Toast.makeText(HomeActivity.this, "服务端错误",Toast.LENGTH_SHORT);} else if (error instanceof AuthFailureError) {Toast.makeText(HomeActivity.this, "验证错误",Toast.LENGTH_SHORT);} else if (error instanceof ParseError) {Toast.makeText(HomeActivity.this, "解析错误",Toast.LENGTH_SHORT);} else if (error instanceof NoConnectionError){Toast.makeText(HomeActivity.this, "无连接错误",Toast.LENGTH_SHORT);} else if (error instanceof TimeoutError) {Toast.makeText(HomeActivity.this, "连接超时",Toast.LENGTH_SHORT);} }};
ImageBitmapCache.java
package com.lzp.elearning.common.Downloader;import android.graphics.Bitmap;
import android.support.v4.util.LruCache;import com.android.volley.toolbox.ImageLoader.ImageCache;public class ImageBitmapCache implements ImageCache
{private LruCache<String,Bitmap> mCache;//圖片緩存的最大空間private final static int maxSize = 8 * 1024 * 1024;public ImageBitmapCache(){mCache = new LruCache<String, Bitmap>(maxSize){@Overrideprotected int sizeOf(String key, Bitmap bitmap) {return bitmap.getRowBytes() * bitmap.getHeight();}};}@Overridepublic Bitmap getBitmap(String url) {return mCache.get(url);}@Overridepublic void putBitmap(String url, Bitmap bitmap) {mCache.put(url, bitmap);}}
这篇关于android框架Volley学习demo的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!