• Android开发之Volley网络通信框架


    今天用了一下Volley网络通信框架,感觉挺好用的,写个博客记录一下用法。方便以后VC。


    Volley(Google提供的网络通信库,能使网络通信更快,更简单,更健壮。)

    功能模块:


    1. JSON,图像等的异步下载
    ---------------------------------------------------------------------------------------
    2. 网络请求的排序(scheduling)
    ---------------------------------------------------------------------------------------
    3. 网络请求的优先级处理
    ---------------------------------------------------------------------------------------
    4. 缓存
    ---------------------------------------------------------------------------------------
    5. 多级别取消请求
    ---------------------------------------------------------------------------------------
    6. 和Activity和生命周期的联动(Activity结束时同一时候取消全部网络请求)




    长处:Volley特别适合数据量不大可是通信频繁的场景。


    Volley的jar包CSDN上有非常多,也能够上GitHub上下载。



    以下是我写的一个简单的样例。


    import org.json.JSONObject; 
    import android.app.Activity; 
    import android.app.ProgressDialog; 
    import android.graphics.Bitmap; 
    import android.os.Bundle; 
    import android.support.v4.util.LruCache; 
    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.ImageCache; 
    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; 
       
    /**
     * Demo描写叙述: 
     *   利用Volley获取JSON数据
     *   利用Volley异步载入图片
     *   利用NetworkImageView载入网路图片
     */ 
    public class MainActivity extends Activity { 
        private ImageView mImageView; 
        private NetworkImageView mNetworkImageView; 
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main); 
            init(); 
        } 
           
        private void init(){ 
            mImageView=(ImageView) findViewById(R.id.imageView); 
            mNetworkImageView=(NetworkImageView)findViewById(R.id.networkImageView); 
            getJSONByVolley(); 
            loadImageByVolley(); 
            showImageByNetworkImageView(); 
        } 
       
        /**
         * 利用Volley获取JSON数据
         */ 
        private void getJSONByVolley() { 
            RequestQueue requestQueue = Volley.newRequestQueue(this); 
            String JSONDataUrl = "要訪问的URL"; 
            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); 
        } 
           
           
        /**
         * 利用Volley异步载入图片
         * 
         * getImageListener(ImageView view, int defaultImageResId, int errorImageResId)
         * 第一个參数:显示图片的ImageView
         * 第二个參数:默认显示的图片资源
         * 第三个參数:载入错误时显示的图片资源
         */ 
        private void loadImageByVolley(){ 
            String imageUrl="要訪问的URL"; 
            RequestQueue requestQueue = Volley.newRequestQueue(this); 
            final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(20); 
            ImageCache imageCache = new ImageCache() { 
                @Override 
                public void putBitmap(String key, Bitmap value) { 
                    lruCache.put(key, value); 
                } 
       
                @Override 
                public Bitmap getBitmap(String key) { 
                    return lruCache.get(key); 
                } 
            }; 
            ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache); 
            ImageListener listener = ImageLoader.getImageListener(mImageView, R.drawable.ic_launcher,R.drawable.ic_launcher); 
            imageLoader.get(imageUrl, listener); 
        } 
           
        /**
         * 利用NetworkImageView显示网络图片
         */ 
        private void showImageByNetworkImageView(){ 
            String imageUrl="要訪问的URL"; 
            RequestQueue requestQueue = Volley.newRequestQueue(this); 
            final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(20); 
            ImageCache imageCache = new ImageCache() { 
                @Override 
                public void putBitmap(String key, Bitmap value) { 
                    lruCache.put(key, value); 
                } 
       
                @Override 
                public Bitmap getBitmap(String key) { 
                    return lruCache.get(key); 
                } 
            }; 
            ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache); 
            mNetworkImageView.setImageUrl(imageUrl,imageLoader); 
        } 
       
    }



    布局文件:


    <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" 
        /> 
           
         <ImageView 
            android:id="@+id/imageView" 
            android:layout_width="120dip" 
            android:layout_height="120dip" 
        /> 
       
    </RelativeLayout>



    记录完成!下次用到直接VC!

  • 相关阅读:
    RecyclerView
    Android动态广播的注册与销毁
    使用FFmpeg让mp4转gif
    当你安装python3没有安装依赖环境yum install -y libffi-devel,pip和setuptools,pip会装不上,不安装yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel lib
    遇到的39.0或者其他版本不匹配的问题,请找对路径
    yaml创建学习(第二天)
    yaml 测试用例 (学习第一天)
    关于二次封装css selector 的复数定位
    uiautomatorviewer报错“Error taking device screenshot: EOF” ,
    appium解决每次运行都需要安装Unlock以及AppiumSetting的问题
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/6933263.html
Copyright © 2020-2023  润新知