安卓高级2 Volley框架的使用案例
初始化类:
MyApp.java
package qianfeng.com.day37_volley_pull.app;
import android.app.Application;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
/**
* Created by ${Mr.Zhao} on 2016/10/18.
*/
public class MyApp extends Application {
//定义一个 请求队列
private static RequestQueue requestQueue;
@Override
public void onCreate() {
super.onCreate();
// 初始化 请求队列
requestQueue = Volley.newRequestQueue(this);
}
// 将消息队列返回
public static RequestQueue getRequestQueue() {
return requestQueue;
}
}
零散并发下载字符数据
package qianfeng.com.day37_volley_pull;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import qianfeng.com.day37_volley_pull.app.MyApp;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.volley_get_bt:
String path = "http://c.m.163.com/nc/article/list/T1370583240249/0-20.html";
// 定义 一次请求
/*
1. 请求方式
2.路径
3.请求成功的回调监听
4.请求错误的时候 回调监听
*/
StringRequest stringRequest = new StringRequest(Request.Method.GET, path, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Mr.Zhao", "onResponse: " + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Mr.Zhao", "onErrorResponse: ");
}
});
// 将 这个请求 添加到 请求队列中!!!!!!
MyApp.getRequestQueue().add(stringRequest);
break;
case R.id.volley_post_bt:
String post_Path = "http://mrobot.pcauto.com.cn/v2/cms/channels/1?";
StringRequest request = new StringRequest(Request.Method.POST, post_Path, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Mr.Zhao", "onResponse: " + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Mr.Zhao", "onErrorResponse: " + error);
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<>();
map.put("pageNo", "1");
map.put("pageSize", "20");
map.put("serialIds", "2143,3404");
map.put("v", "4.0.0");
return map;
}
};
MyApp.getRequestQueue().add(request);
break;
case R.id.volley_object_bt:
String object_path = "http://mrobot.pcauto.com.cn/v2/cms/channels/1?";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, object_path, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("Mr.Zhao", "onResponse: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<>();
map.put("pageNo", "1");
map.put("pageSize", "20");
map.put("serialIds", "2143,3404");
map.put("v", "4.0.0");
return map;
}
};
jsonObjectRequest.setTag("object");
MyApp.getRequestQueue().add(jsonObjectRequest);
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// 取消标记为 object 的 所有的请求
MyApp.getRequestQueue().cancelAll("object");
}
}
零散并发下载图片:
package qianfeng.com.day37_volley_pull;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.util.LruCache;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import qianfeng.com.day37_volley_pull.app.MyApp;
public class ImageActivity extends AppCompatActivity {
private ImageView show_iv;
//2 使用缓存 Loader的方式加载图片
//2.1 定义一个LruCache
LruCache<String, Bitmap> lru;
ImageLoader loader;
private NetworkImageView netImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
show_iv = (ImageView) findViewById(R.id.show_iv);
initLoaderImage();
//3. 使用 Volley框架 提供的 NetWorkImageView 来加载一张图片
netImageView = (NetworkImageView) findViewById(R.id.netImageView);
netImageView.setDefaultImageResId(R.mipmap.ic_launcher);
// 3.1 加载图片 需要 先 初始化 ImageLoader
netImageView.setImageUrl("http://img5.duitang.com/uploads/item/201511/02/20151102010720_yXnhP.thumb.224_0.jpeg",loader);
}
// s使用Loader 加载图片的方式
private void initLoaderImage() {
//2.2 实例化Lru
int size = (int) (Runtime.getRuntime().maxMemory() / 8);
lru = new LruCache<String, Bitmap>(size) {
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();
}
};
//2.3 实例化一个Loader
loader = new ImageLoader(MyApp.getRequestQueue(), new ImageLoader.ImageCache() {
@Override
public Bitmap getBitmap(String url) {
return lru.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
lru.put(url, bitmap);
}
});
}
//1. 使用请求的方式加载图片
public void onClick(View view) {
String path = "http://imgsrc.baidu.com/baike/pic/item/dcc451da81cb39db1a2e7b6ed5160924ab18301f.jpg";
ImageRequest request = new ImageRequest(path, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
if (response != null)
show_iv.setImageBitmap(response);
else
Toast.makeText(ImageActivity.this, "没图片", Toast.LENGTH_SHORT).show();
}
}, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, Bitmap.Config.RGB_565, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
MyApp.getRequestQueue().add(request);
}
//2.3 加载图片
public void loaderImage(View view) {
String imagePath = "http://tupian.enterdesk.com/2013/mxy/10/12/3/5.jpg";
// 获取监听器
ImageLoader.ImageListener listener = loader.getImageListener(show_iv, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
// 获取图片
loader.get(imagePath, listener);
}
}
零散并发下载图片对应布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="qianfeng.com.day37_volley_pull.ImageActivity">
<Button
android:id="@+id/image_bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="获取网络图片" />
<Button
android:id="@+id/loader_bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loaderImage"
android:text="Loader" />
<ImageView
android:id="@+id/show_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/netImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>