网络通信框架Volley

简介:
• 并发、效率、性能 高要求
• Volley(齐射,迸发)
• Volley是Google2013发布的Android平台上的网络通信库
特点:
• 通信更快,更简单,更健壮
• Get、Post网络请求及网络图像的高效率异步处理请求
• 对网络请求进行排序优先级处理
• 网络请求的缓存
• 多级别取消请求(同时取消正在进行的多个网络请求)
• 和Activity生命周期的联动(当Activity销毁的时候可以同时取消 正在进行的网络请求操作,提高性能)

布局文件
···
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.myvolleydemo.MainActivity">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <Button
            android:id="@+id/but_get"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Get请求字符串" />
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/but_Img_Request"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ImgRequest请求图片" />
        <Button
            android:id="@+id/but_Img_Loader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Loader请求图片" />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Imagev"
            android:src="@mipmap/ic_launcher"/>
        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/newwork"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</ScrollView>

</LinearLayout>

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button Get_Btn;
private Button Post_Btn;
private Button Image_Btn;
private ImageView get_Image;
private String mUrl = "http://112.124.22.238:8081/course_api/cniaoplay/featured2?p={%27page%27:0}";
private String mPostUrl = "http://172.16.45.10:8080/FuWebDemo/AA";
private TextView mText;
private NetworkImageView mNet;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
}

private void initView() {
    Get_Btn = (Button) findViewById(R.id.Get_Btn);
    Post_Btn = (Button) findViewById(R.id.Post_Btn);
    Image_Btn = (Button) findViewById(R.id.Image_Btn);
    get_Image = (ImageView) findViewById(R.id.get_Image);
    mText = (TextView) findViewById(R.id.Get_Text);
    Get_Btn.setOnClickListener(this);
    Post_Btn.setOnClickListener(this);
    Image_Btn.setOnClickListener(this);
    mNet = (NetworkImageView) findViewById(R.id.NetImage);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.Get_Btn:
            getInfo();
            break;
        case R.id.Post_Btn:
            postInfo();
            break;
        case R.id.Image_Btn:

// getImage();
// getImageLoader();
getNetImage();
break;
}
}

//get请求数据
private void getInfo() {
    RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
    StringRequest stringRequest = new StringRequest(StringRequest.Method.GET, mUrl, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            mText.setText(response + "");
        }
    }, null);
    requestQueue.add(stringRequest);
}

//Post请求数据
private void postInfo() {
    RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, mPostUrl, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            mText.setText(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    }) {
        //因为是POST请求,构造方法里面没有参数  他们给封装到了构造方法后边
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> map = new HashMap<String, String>();
            map.put("userName", "lxx");
            map.put("passWord", "123");
            return map;
        }
    };
    requestQueue.add(stringRequest);
}

//ImageRequest请求图片
private void getImage() {
    RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
    //参数1:图片的网址,参数2:成功回调 参数3,4:图片的最大宽和高 参数5:设置图片样式,
    ImageRequest imageRequest = new ImageRequest(ImageUrls.imageUrls[3], new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap response) {
            get_Image.setImageBitmap(response);
        }
    }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    requestQueue.add(imageRequest);
}

//ImageLoader
private void getImageLoader() {
    RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
    ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
        @Override
        public Bitmap getBitmap(String url) {
            Log.e("url", url);
            return null;
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            Log.e("putBitmap", url);
        }
    });
    //设置请求回调  参数1:代表的是回调成功吧图片放到那个组件上,参数2:请求时加载的默认图片,参数3:请求失败的图片
    ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(get_Image, R.mipmap.iv_lol_icon3, R.mipmap.iv_lol_icon14);
    imageLoader.get(ImageUrls.imageUrls[2] + "dsfsdf", imageListener);
}

//Volley里面提供一个组件可以用来显示Image  NetWorkImageView
private void getNetImage() {
    RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
    ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
        @Override
        public Bitmap getBitmap(String url) {
            Log.e("url", url);
            return null;
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {
            Log.e("putBitmap", url);
        }
    });
    mNet.setDefaultImageResId(R.mipmap.iv_lol_icon3);
    mNet.setErrorImageResId(R.mipmap.iv_lol_icon14);
    mNet.setImageUrl(ImageUrls.imageUrls[8], imageLoader);
}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容