Volley框架用法讲解

基本用法讲解

1、StringRequest的用法

        private RequestQueue mQueue;
private StringRequest mRequest;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_string_request);
mTextView = (TextView) findViewById(R.id.display_txt);
mQueue = Volley.newRequestQueue(this);
TestSimpleStringReguest2();
mQueue.add(mRequest);
}


private void TestSimpleStringReguest() {
//默认Method.GET
mRequest = new StringRequest("http://www.sina.com.cn", new Response.Listener() {


@Override
public void onResponse(String response) {
// TODO Auto-generated method stub
mTextView.setText(response);
Log.e(TAG, "请求结果:" + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.e(TAG, "出错啦:" + error.getMessage());
}
});
}

private void TestSimpleStringReguest2() {
//选择Method
mRequest = new StringRequest(Method.GET, "http://www.sina.com.cn", new Response.Listener() {


@Override
public void onResponse(String response) {
// TODO Auto-generated method stub
mTextView.setText(response);
Log.e(TAG, "请求结果:" + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.e(TAG, "出错啦:" + error.getMessage());
}
});
}


2、JsonObjectRequest的用法  

private RequestQueue newRequestQueue;
private JsonObjectRequest jsonObjectRequest;
private TextView mTextView;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json_request);
mTextView = (TextView) findViewById(R.id.display_txt);
newRequestQueue = Volley.newRequestQueue(JsonObjectRequestActivity.this);
test1();
newRequestQueue.add(jsonObjectRequest);
}


private void test1() {
jsonObjectRequest = new JsonObjectRequest(
"http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=101010100&weatherType=0", null,
new Response.Listener() {


@Override
public void onResponse(JSONObject response) {
mTextView.setText(response.toString());
Log.e("TAG", response.toString());
}
}, new Response.ErrorListener() {


@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
}
});
}

3、JsonArrayRequest的用法

private RequestQueue newRequestQueue;
private JsonArrayRequest jsonArrayRequest;
private TextView mTextView;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json_request);
mTextView = (TextView) findViewById(R.id.display_txt);
newRequestQueue = Volley.newRequestQueue(this);
test1();
newRequestQueue.add(jsonArrayRequest);
}


private void test1() {
jsonArrayRequest = new JsonArrayRequest(
"http://weather.51wnl.com/weatherinfo/GetMoreWeather?cityCode=101010100&weatherType=0",
new Response.Listener() {


@Override
public void onResponse(JSONArray response) {
mTextView.setText(response.toString());
Log.e("TAG", response.toString());
}
}, new Response.ErrorListener() {


@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
}
});
}

        注意:JsonArrayRequest 和 JsonObjectRequest 继承JsonRequest,三者使用方法基本一致

 4、ImageRequest的用法

ImageView mImageView;
RequestQueue newRequestQueue;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imagerequest);
mImageView = (ImageView) findViewById(R.id.display_im);
newRequestQueue = Volley.newRequestQueue(ImageRequestActivity.this);
newRequestQueue.add(imageRequest);
}


// ImageRequest的用法
@SuppressWarnings("deprecation")
ImageRequest imageRequest = new ImageRequest("http://imgt6.bdstatic.com/it/u=2,887966933&fm=19&gp=0.jpg",
new Response.Listener() {


@Override
public void onResponse(Bitmap response) {
mImageView.setImageBitmap(response);
}
}, 0, 0, Config.RGB_565, null);

 5、ImageLoader用法

private ImageView mImageView;
private RequestQueue newRequestQueue;
private ImageLoader imageLoader;
private ImageListener listener;
@Override

//ImageLoader用法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imageloader);
mImageView = (ImageView) findViewById(R.id.display_im);
newRequestQueue = Volley.newRequestQueue(ImageLoaderActivity.this);
imageLoader = new ImageLoader(newRequestQueue, new ImageCache() {  
   @Override  
   public void putBitmap(String url, Bitmap bitmap) {  
   }  
 
   @Override  
   public Bitmap getBitmap(String url) {  
       return null;  
   }  
});  
listener = ImageLoader.getImageListener(mImageView, R.drawable.ic_launcher, R.drawable.ic_launcher);
//imageLoader.get("https://img-my.csdn.net/uploads/201404/13/1397393290_5765.jpeg", listener);
imageLoader.get("https://img-my.csdn.net/uploads/201404/13/1397393290_5765.jpeg", listener, 200, 200);
}

6、NetworkImageView的用法

      ImageLoader用法 + BitmapCache缓存

private NetworkImageView networkImageView;
private RequestQueue newRequestQueue;
private ImageLoader imageLoader;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_network_imageview);
networkImageView  = (NetworkImageView) findViewById(R.id.network_image_view);
newRequestQueue = Volley.newRequestQueue(NetworkImageViewActivity.this);
imageLoader = new ImageLoader(newRequestQueue, new BitmapCache());
networkImageView.setDefaultImageResId(R.drawable.ic_launcher);  
networkImageView.setErrorImageResId(R.drawable.ic_launcher);  
networkImageView.setImageUrl("https://img-my.csdn.net/uploads/201404/13/1397393290_5765.jpeg",  
               imageLoader); 
}


}


class BitmapCache implements ImageCache {  
 
    private LruCache mCache;  
  
    public BitmapCache() {  
        int maxSize = 10 * 1024 * 1024;  
        mCache = new LruCache(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);  
    }  
  
}  


layout布局

            android:id="@+id/network_image_view"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal" />

你可能感兴趣的:(开源框架详解)