您的位置:首页 > 移动开发 > Android开发

android-google Volley framwork(2)

2016-01-11 12:06 357 查看
> how to use the common request types that Volley supports:

StringRequest
. Specify a URL and receive a raw string in response. See Setting
Up a Request Queue for an example.
ImageRequest
. Specify a URL and receive an image in response.

JsonObjectRequest
and
JsonArrayRequest
(both subclasses of
JsonRequest
). Specify a URL and get a JSON object or array (respectively) in response.

Here is a sample implementation for an in-memory
LruBitmapCache
class. It extends the
LruCache
class
and implements the
ImageLoader.ImageCache
interface:
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.util.DisplayMetrics;
import com.android.volley.toolbox.ImageLoader.ImageCache;

public class LruBitmapCache extends LruCache<String, Bitmap>
implements ImageCache {

public LruBitmapCache(int maxSize) {
super(maxSize);
}

public LruBitmapCache(Context ctx) {
this(getCacheSize(ctx));
}

@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight();
}

@Override
public Bitmap getBitmap(String url) {
return get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}

// Returns a cache size equal to approximately three screens worth of images.
public static int getCacheSize(Context ctx) {
final DisplayMetrics displayMetrics = ctx.getResources().
getDisplayMetrics();
final int screenWidth = displayMetrics.widthPixels;
final int screenHeight = displayMetrics.heightPixels;
// 4 bytes per pixel
final int screenBytes = screenWidth * screenHeight * 4;

return screenBytes * 3;
}
}


Here is an example of how to instantiate an
ImageLoader
to use this cache:
RequestQueue mRequestQueue; // assume this exists.
ImageLoader mImageLoader = new ImageLoader(mRequestQueue, new LruBitmapCache(
LruBitmapCache.getCacheSize()));


使用Gson解析Json数据。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: