簡介:
? 并發(fā)、效率、性能 高要求
? Volley(齊射,迸發(fā))
? Volley是Google2013發(fā)布的Android平臺上的網(wǎng)絡(luò)通信庫
特點:
? 通信更快,更簡單,更健壯
? Get、Post網(wǎng)絡(luò)請求及網(wǎng)絡(luò)圖像的高效率異步處理請求
? 對網(wǎng)絡(luò)請求進(jìn)行排序優(yōu)先級處理
? 網(wǎng)絡(luò)請求的緩存
? 多級別取消請求(同時取消正在進(jìn)行的多個網(wǎng)絡(luò)請求)
? 和Activity生命周期的聯(lián)動(當(dāng)Activity銷毀的時候可以同時取消 正在進(jìn)行的網(wǎng)絡(luò)請求操作,提高性能)
布局文件
···
<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請求數(shù)據(jù)
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請求數(shù)據(jù)
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請求,構(gòu)造方法里面沒有參數(shù) 他們給封裝到了構(gòu)造方法后邊
@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);
//參數(shù)1:圖片的網(wǎng)址,參數(shù)2:成功回調(diào) 參數(shù)3,4:圖片的最大寬和高 參數(shù)5:設(shè)置圖片樣式,
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);
}
});
//設(shè)置請求回調(diào) 參數(shù)1:代表的是回調(diào)成功吧圖片放到那個組件上,參數(shù)2:請求時加載的默認(rèn)圖片,參數(shù)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);
}
}