Android 의 Volley 사용

14801 단어 android
다음으로 이동:http://blog.csdn.net/lfdfhl/article/details/12223345
         
/** 
     *   NetworkImageView       
     */  
    private void showImageByNetworkImageView(){  
        String imageUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";  
        RequestQueue requestQueue = Volley.newRequestQueueInDisk(ctx, "/sdcard/demo/", null);
        ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache());  
        
        mNetworkImageView.setTag("url");  
        mNetworkImageView.setImageUrl(imageUrl,imageLoader);  
    }  

 
volley 원본 파일:volley.zip
volley 의 jar 파일:Volley.jar.zip
 
사용 예:
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.ImageView;
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.ImageLoader.ImageListener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

public class MainActivity extends Activity {

    private Context ctx;  
    private RequestQueue mQueue;  
    private ImageLoader mImageLoader;
    
    private ImageView mImageView;  
    private NetworkImageView mNetworkImageView;  
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        
        this.ctx = this; 
        initView();
        
        showImageByNetworkImageView();
    }
    
    private void initView(){
        mImageView=(ImageView) findViewById(R.id.imageView);  
        mNetworkImageView=(NetworkImageView)findViewById(R.id.networkImageView);
    }
    
    /** 
     *   Volley  JSON   
     */  
    private void getJSONByVolley() {  
        RequestQueue requestQueue = Volley.newRequestQueue(this);  
        String JSONDataUrl = "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json";  
        final ProgressDialog progressDialog = ProgressDialog.show(this, "This is title", "...Loading...");  
  
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(  
                Request.Method.GET,   
                JSONDataUrl,   
                null,  
                new Response.Listener<JSONObject>() {  
                    @Override  
                    public void onResponse(JSONObject response) {  
                        System.out.println("response="+response);  
                        if (progressDialog.isShowing()&&progressDialog!=null) {  
                            progressDialog.dismiss();  
                        }  
                    }  
                },   
                new Response.ErrorListener() {  
                    @Override  
                    public void onErrorResponse(VolleyError arg0) {  
                           System.out.println("sorry,Error");  
                    }  
                });  
        requestQueue.add(jsonObjectRequest); 
    }  

    private void loadImageByVolley(){  
        String imageUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";  
        RequestQueue requestQueue = Volley.newRequestQueue(this);  
       
        ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache());  
        ImageListener listener = ImageLoader.getImageListener(mImageView, R.drawable.ic_launcher,R.drawable.ic_launcher);  
        imageLoader.get(imageUrl, listener);  
    }  
      
    /** 
     *   NetworkImageView       
     */  
    private void showImageByNetworkImageView(){  
        String imageUrl="http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";  
        RequestQueue requestQueue = Volley.newRequestQueueInDisk(ctx, "/sdcard/demo/", null);
        ImageLoader imageLoader = new ImageLoader(requestQueue, new BitmapCache());  
        
        mNetworkImageView.setTag("url");  
        mNetworkImageView.setImageUrl(imageUrl,imageLoader);  
    }  

}

 
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import com.android.volley.toolbox.ImageLoader.ImageCache;

public class BitmapCache implements 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 value) {  
                return value.getRowBytes() * value.getHeight();  
            }  
        };  
    }  

    @Override  
    public Bitmap getBitmap(String url) {  
        return mCache.get(url);  
    }  

    @Override  
    public void putBitmap(String url, Bitmap bitmap) {  
        mCache.put(url, bitmap);  
    }  
}

 
<RelativeLayout 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"  
     >  
  
    <com.android.volley.toolbox.NetworkImageView  
        android:id="@+id/networkImageView"  
        android:layout_width="120dip"  
        android:layout_height="120dip"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="30dip"  
    />  
      
     <ImageView  
        android:id="@+id/imageView"  
        android:layout_width="120dip"  
        android:layout_height="120dip"  
        android:layout_centerInParent="true"  
    />  
  
</RelativeLayout> 

좋은 웹페이지 즐겨찾기