工具:Android Studio
添加依赖
初始化一个全局的RequestQueue
public class MyApp extends Application {
public static RequestQueue requestQueue;
@Override
public void onCreate() {
super.onCreate();
requestQueue = Volley.newRequestQueue(getApplicationContext());
}
}
StringRequest
1、GET
StringRequest stringRequest = new StringRequest(Request.Method.GET,urlPath, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
response = new String(response.getBytes("ISO-8859-1"),"UTF-8");//处理中文乱码
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Log.e(TAG, "onResponse: " + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "onErrorResponse: " + error.toString());
}
});
stringRequest.setTag("stringGet");
MyApp.requestQueue.add(stringRequest);
2、POST
StringRequest stringRequest = new StringRequest(Request.Method.POST,urlPath, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
response = new String(response.getBytes("ISO-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Log.e(TAG, "onResponse: " + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "onErrorResponse: " + error.toString());
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("area","深圳");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("","");
return params;
}
};
stringRequest.setTag("stringPost");
MyApp.requestQueue.add(stringRequest);
JsonObjectRequest
1、GET
JsonObjectRequest jsonRequestGet = new JsonObjectRequest(Request.Method.GET,urlPath,null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
response = new JSONObject(new String(response.toString().getBytes("ISO-8859-1"),"UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
Log.e(TAG, "onResponse: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "onErrorResponse: " + error.toString());
}
});
jsonRequestGet.setTag("jsonRequestGet");
MyApp.requestQueue.add(jsonRequestGet);
Map<String, String> params = new HashMap<String, String>();
params.put("area","北京");
JsonObjectRequest jsonRequestPOST = new JsonObjectRequest(Request.Method.POST, urlPath,new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
response = new JSONObject(new String(response.toString().getBytes("ISO-8859-1"),"UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
Log.e(TAG, "onResponse: " + response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "onErrorResponse: " + error.toString());
}
}
);
jsonRequestPOST.setTag("jsonRequestPOST");
MyApp.requestQueue.add(jsonRequestPOST);
加载图片
1、ImageRequest
ImageRequest imageRequest = new ImageRequest(
"pictureUrl",
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
imageView.setImageBitmap(response);
}
}, 0, 0, Bitmap.Config.RGB_565,
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
imageView.setImageResource(R.mipmap.ic_launcher);
}
});
MyApp.requestQueue.add(imageRequest);
2、ImageLoader
public class BitmapCache implements ImageLoader.ImageCache {
private LruCache<String, Bitmap> mCache;
public BitmapCache() {
int maxSize = 10 * 1024 * 1024;
mCache = new LruCache<String, Bitmap>(maxSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight();
}
};
}
@Override
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
}
ImageLoader imageLoader = new ImageLoader(MyApp.getInstance().getRequestQueue(),new BitmapCache());
//第一个参数指定用于显示图片的ImageView控件,第二个参数指定加载图片的过程中显示的图片,第三个参数指定加载图片失败的情况下显示的图片。
ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView,R.mipmap.ic_launcher, R.mipmap.ic_launcher);
imageLoader.get("pictureUrl", listener);
// imageLoader.get("pictureUrl",listener, 200, 200);//设置图片大小
3、NetworkImageView控件
<com.android.volley.toolbox.NetworkImageView android:id="@+id/networkImageView" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
networkImageView.setDefaultImageResId(R.mipmap.ic_launcher); networkImageView.setErrorImageResId(R.mipmap.ic_launcher); networkImageView.setImageUrl("pictureUrl",new ImageLoader(MyApp.requestQueue,new BitmapCache()));
通过TAG取消请求
@Override protected void onDestroy() { super.onDestroy(); MyApp.requestQueue.cancelAll("stringGet"); MyApp.requestQueue.cancelAll("stringPost"); }