Volley
来源:互联网 发布:js插件如何编写 编辑:程序博客网 时间:2024/05/16 23:49
MyApplication
public class MyApplication extends Application { public static RequestQueue requestQueue; @Override public void onCreate() { super.onCreate(); //实例化RequestQueue对象 requestQueue= Volley.newRequestQueue(getApplicationContext()); } //返回该实例化对象 public static RequestQueue getRequestQueue(){ return requestQueue; }}
MyImageCatch
/** * Created by cold on 15/9/3. */public class MyImageCatch implements ImageCache { public LruCache<String, Bitmap> cache; public int maxSize = 10 * 1024 * 1024; public MyImageCatch() { cache = new LruCache<String, Bitmap>(maxSize) { @Override protected int sizeOf(String key, Bitmap value) { return value.getRowBytes() * value.getHeight(); } }; } @Override public Bitmap getBitmap(String s) { return cache.get(s); } @Override public void putBitmap(String s, Bitmap bitmap) { cache.put(s, bitmap); }}
public class FirstActivity extends AppCompatActivity { String stringUrl = "http://192.168.0.52:8888/info.php"; String jsonURL = "http://192.168.0.52:8888/json.php"; String imageUrl = "http://192.168.0.52:8888/pic101.jpg"; ImageView imageView; NetworkImageView networkImageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_first); imageView = (ImageView) findViewById(R.id.imageView); networkImageView= (NetworkImageView) findViewById(R.id.networkImageView); } //获取图片并缓存,该方法在主线程中使用 public void catchImage2(String urlString) { //networkImageView为Volley包中的控件 ImageLoader imageLoader = new ImageLoader(MyApplication.getRequestQueue(), new MyImageCatch()); //设置默认显示图片 networkImageView.setDefaultImageResId(R.drawable.loadf); //获取失败显示的图片 networkImageView.setErrorImageResId(R.drawable.loadfail); //开始通过URL字符串获取图片 networkImageView.setImageUrl(urlString,imageLoader); } //获取图片并缓存,该方法在主线程中使用 public void catchImage1(String urlString) { ImageLoader imageLoader = new ImageLoader(MyApplication.getRequestQueue(), new MyImageCatch()); //参数为Imageview,默认图片,出错时图片 ImageLoader.ImageListener imageListener=ImageLoader.getImageListener(imageView,R.drawable.loadf,R.drawable.loadfail); //开始通过URL字符串获取图片 imageLoader.get(urlString,imageListener); } //获取图片,可直接在主线程中调用 private void getImage(String urlString) { ImageRequest imageRequest = new ImageRequest(urlString, new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap bitmap) { imageView.setImageBitmap(bitmap); } }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { imageView.setImageResource(R.drawable.loadfail); } }); imageRequest.setTag("getImage"); MyApplication.getRequestQueue().add(imageRequest); } private void post_methord() { volley_postString(); volley_postJSON(); MyApplication.getRequestQueue().cancelAll("setTag设置的标签"); } //post方式获取json(不可用),可使用获取字符串的post方法 private void volley_postJSON() { HashMap<String, String> hashMap = new HashMap<String, String>(); hashMap.put("info", "volley_postJSON"); JSONObject jsonObject = new JSONObject(hashMap); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, jsonURL, jsonObject, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { Log.e("volley_postJSON_suce", jsonObject.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Log.e("volley_postJSON_fail", volleyError.toString()); } }); jsonObjectRequest.setTag("volley_postJSON"); MyApplication.getRequestQueue().add(jsonObjectRequest); } //post方式获取字符串 private void volley_postString() { StringRequest postStringRequest = new StringRequest(Request.Method.POST, stringUrl, new Response.Listener<String>() { @Override public void onResponse(String string) { Log.e("volley_postString_suce", string); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Log.e("volley_postString_fail", volleyError.toString()); } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<String, String>(); map.put("info", "volley_postString"); return map; } }; postStringRequest.setTag("volley_postString"); MyApplication.getRequestQueue().add(postStringRequest); } private void get_methord() { voller_getString(); volley_getJSON(); } //get获取json private void volley_getJSON() { JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, jsonURL + "?info=volley_getJSON", null, new Response.Listener() { @Override public void onResponse(Object o) { Log.e("volley_getJSON_sucess", o.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Log.e("volley_getJSON_error", volleyError.toString()); } }); jsonObjectRequest.setTag("volley_getJSON"); MyApplication.getRequestQueue().add(jsonObjectRequest); } //get获取字符串 private void voller_getString() { StringRequest stringRequest = new StringRequest(Request.Method.GET, stringUrl + "?info=see", new Response.Listener<String>() { @Override public void onResponse(String string) { Toast.makeText(FirstActivity.this, string, Toast.LENGTH_SHORT).show(); Log.e("voller_getString_sucess", string); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError string) { Toast.makeText(FirstActivity.this, string.toString(), Toast.LENGTH_SHORT).show(); Log.e("voller_getString_fail", string.toString()); } }); //设置Tag标记请求,请求队列调用.cancelAll("voller_getString")取消下面的请求 stringRequest.setTag("voller_getString"); MyApplication.getRequestQueue().add(stringRequest); }}
0 0
- Volley
- Volley
- volley
- volley
- Volley
- Volley
- volley
- Volley
- Volley
- volley
- Volley
- volley
- Volley
- Volley
- Volley
- volley
- Volley
- volley
- 《UNIX环境高级编程》学习笔记--第三章习题2参考答案
- 精品软件 推荐 SSD固态硬盘潜能释放器 SSDRunner
- C++中const的实现细节
- 系统瓶颈的定位
- 【HDU4185】【最大二分匹配】【无法确定uN,vN就结果/2】
- Volley
- HDOJ 4081 Qin Shi Huang's National Road System (次小生成树变形--prime)
- 引用与指针有什么区别?
- 34. PHP 类
- JS验证正则表达式(大全)
- 1058. A+B in Hogwarts (20)
- 多线程一定快吗
- appium源码分析(十)-GetAttribute
- IOS开发UI—纯代码实现控件及其基本属性