Volley是一个轻量级的网络请求框架,主要包括字符串请求、json对象请求、图片加载和缓存图片加载四个内容。四个方面大致相同。json请求其实也算是字符串请求,我们一般习惯拿到json字符串之后自己去解析。
首先下载volley.jar,把它放到libs,不需要add。
在清单文件中添加权限。
<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>布局文件几乎都是按钮,另加一个图片控件。最后就是主程序部分。贴出代码,一目了然。
public class MainActivity extends AppCompatActivity { RequestQueue requestQueue = null;//请求队列 @Bind(R.id.btn_string_request) Button btnStringRequest; @Bind(R.id.btn_json_object_request) Button btnJsonObjectRequest; @Bind(R.id.btn_image_request) Button btnImageRequest; @Bind(R.id.btn_image_rloader) Button btnImageRloader; @Bind(R.id.img_icon) ImageView imgIcon; @Override PRotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); requestQueue = Volley.newRequestQueue(this); } @OnClick({R.id.btn_string_request, R.id.btn_json_object_request, R.id.btn_image_request, R.id.btn_image_rloader}) public void onClick(View view) { switch (view.getId()) { case R.id.btn_string_request: stringRequest(); break; case R.id.btn_json_object_request: jsonObjectRequest(); break; case R.id.btn_image_request: imageRequest(); break; case R.id.btn_image_rloader: imageLoad(); break; } } /** * imageLoader缓存加载图片 */ private void imageLoad() { //请求地址 String url = "http://www.86kx.com/uploads/allimg/161017/2301_161017093243_1.jpg"; ImageLoader imageLoader = new ImageLoader(requestQueue, new MyImageCache()); ImageLoader.ImageListener listener = imageLoader.getImageListener(imgIcon, R.mipmap.ic_launcher, R.mipmap.ic_launcher); imageLoader.get(url, listener, 0, 0); } /** * 自定义图片缓存 */ private class MyImageCache implements ImageLoader.ImageCache { private LruCache<String, Bitmap> lruCache; private int max = 10 * 1024 * 1024;//缓存大小 public MyImageCache() { lruCache = new LruCache<String, Bitmap>(max) { @Override protected int sizeOf(String key, Bitmap bitmap) { //缓存大小为图片每一行的字节数乘以高度 return bitmap.getRowBytes() * bitmap.getHeight(); } }; } @Override public Bitmap getBitmap(String s) { return lruCache.get(s); } @Override public void putBitmap(String s, Bitmap bitmap) { lruCache.put(s, bitmap); } } /** * 加载网络图片 */ private void imageRequest() { //请求地址 String url = "http://www.86kx.com/uploads/allimg/161017/2301_161017093243_1.jpg"; //成功回调 Response.Listener<Bitmap> listener = new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap bitmap) { imgIcon.setImageBitmap(bitmap); } }; //错误回调 Response.ErrorListener errorListener = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { imgIcon.setImageResource(R.mipmap.ic_launcher); } }; //不带参数的请求 ImageRequest imageRequest = new ImageRequest(url, listener, 0, 0, Bitmap.Config.RGB_565, errorListener); requestQueue.add(imageRequest);//加入请求队列 } /** * 请求json对象 */ private void jsonObjectRequest() { //请求地址 String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=10&offset=0"; //成功回调 Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject s) { Log.i("成功获得的json字符串", s.toString()); } }; //错误回调 Response.ErrorListener errorListener = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { } }; //不带参数的请求 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, listener, errorListener); requestQueue.add(jsonObjectRequest);//加入请求队列 } /** * 请求字符串 */ private void stringRequest() { //请求地址 String url = "https://www.baidu.com"; //成功回调 Response.Listener<String> listener = new Response.Listener<String>() { @Override public void onResponse(String s) { Log.i("成功获得的字符串", s); } }; //错误回调 Response.ErrorListener errorListener = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { } }; //不带参数的请求 StringRequest stringRequest = new StringRequest(url, listener, errorListener); //带参数的请求,这里不进行详细演示// StringRequest stringRequest2=new StringRequest(Request.Method.POST,url,listener,errorListener){//// @Override// protected Map<String, String> getParams() throws AuthFailureError {// Map<String,String> map=new HashMap<>();// map.put("name","john");// map.put("age","37");// return map;// }// }; requestQueue.add(stringRequest);//加入请求队列 }}运行结果:
新闻热点
疑难解答