package com.example.cuboo.myvolley;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.LruCache;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
/*
* Volley is Android Internet Communication Library with faster,more simple and robust。
* Volley Function:
* 1、JSON,Pictures(AsyncTask)
* 2、Sort of The Internet request
* 3、Solved Priorities of Internet request
* 4、Cache
* 5、Cancel request of Levels
* 6、与Activity的生命周期联动
*
* 获取Volley
*/
public class MainActivity extends AppCompatActivity {
TextView tv;
ImageView img;
NetworkImageView net_img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
getJSONVolley();
loadImageVolley();
loadnetImageVolley();
}
private void init(){
tv = (TextView) findViewById(R.id.tv);
img = (ImageView) findViewById(R.id.img);
net_img = (NetworkImageView) findViewById(R.id.net_img);
}
//获取JSON数据
private void getJSONVolley(){
RequestQueue requestQueue = Volley.newRequestQueue(this);
String JSONDataUrl = "http://www.cnblogs.com/shyy/archive/2012/04/05/2453031.html";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, JSONDataUrl, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
System.out.println("response" + jsonObject);
tv.setText("response" + jsonObject);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
System.out.println("Sorry There is a problem");
tv.setText("有问题!");
}
});
requestQueue.add(jsonObjectRequest);
}
private void loadImageVolley(){
String ImageUrl = "http://s9.sinaimg.cn/middle/002nMbZlzy6NEH8BQRa28&690";
RequestQueue requestQueue = Volley.newRequestQueue(this);
final LruCache<String,Bitmap> lruCache = new LruCache<String,Bitmap>(20);
ImageLoader.ImageCache imageCache = new ImageLoader.ImageCache() {
@Override
public Bitmap getBitmap(String s) {
return lruCache.get(s);
}
@Override
public void putBitmap(String s, Bitmap bitmap) {
lruCache.put(s,bitmap);
}
};
ImageLoader imageLoader = new ImageLoader(requestQueue,imageCache);
ImageLoader.ImageListener listener = ImageLoader.getImageListener(img,R.mipmap.ic_launcher,R.mipmap.ic_launcher);
imageLoader.get(ImageUrl,listener);
}
private void loadnetImageVolley(){
String ImageUrl = "http://image.tianjimedia.com/uploadImages/2012/004/6481LRQE84D2.jpg";
RequestQueue requestQueue = Volley.newRequestQueue(this);
final LruCache<String,Bitmap> lruCache = new LruCache<String,Bitmap>(20);
ImageLoader.ImageCache imageCache = new ImageLoader.ImageCache() {
@Override
public Bitmap getBitmap(String s) {
return lruCache.get(s);
}
@Override
public void putBitmap(String s, Bitmap bitmap) {
lruCache.put(s,bitmap);
}
};
ImageLoader imageLoader = new ImageLoader(requestQueue,imageCache);
net_img.setTag("url");
net_img.setImageUrl(ImageUrl,imageLoader);
}
}
Volley的使用
最新推荐文章于 2020-01-07 15:54:36 发布