前言:
Volley概述:
Volley既可以访问网络取得数据,也可以加载图片,并且在性能方面进行了大幅度的调整。它的设计目标就是适合进行数据量不大但通信频繁 的网络操作。而对于大数据量的网络操作,比如说下载文件等,Volley的表现却非常糟糕。
使用Volley前,需要在项目的build.gradle中引入相应的依赖:
implementation 'com.android.volley:volley:1.2.1'
一、使用Volley进行GET请求:
1.创建activity_volley.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?> <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=".activity.VolleyActivity"> <Button android:id="@+id/btn_volley_get" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="使用Volley进行默认的GET请求" /> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
2.VolleyActivity代码如下:
public class VolleyActivity extends AppCompatActivity { private Button btn_volley_get; private TextView textView; private static final String TAG = "VolleyActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_volley); btn_volley_get = findViewById(R.id.btn_volley_get); textView = findViewById(R.id.textView); btn_volley_get.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://www.baidu.com" , new Response.Listener<String>() { @Override public void onResponse(String response) { textView.setText(response);//请求的结果为百度界面的HTML文件 } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.i(TAG, error.getMessage(), error); } }); //将请求添加到请求队列中 requestQueue.add(stringRequest); } }); } }
具体效果如下:
二、使用Volley进行POST请求:
这里返回的数据是JSON格式,所以需要将JSON字符串转换成对应的Java实体类。
http://www.bejson.com/json2javapojo/ 这个网址可以将json字符串,转换为Java实体类。
实体类IpModel如下:
public class IpModel { @SerializedName("code") private int code; @SerializedName("data") private String data; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getData() { return data; } public void setData(String data) { this.data = data; } }
之后在VolleyActivity类中,点击按钮进行POST请求,代码如下:
public class VolleyActivity extends AppCompatActivity { private Button btn_volley_get; private static final String TAG = "VolleyActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_volley); btn_volley_get = findViewById(R.id.btn_volley_get); btn_volley_get.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST , "https://ip.taobao.com/service/getIpInfo.php?ip=59.108.54.37", null , new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { IpModel ipModel = new Gson().fromJson(response.toString(), IpModel.class); if (null != ipModel) { Log.i(TAG, "data: " + ipModel.getData()); Log.i(TAG, "code:" + ipModel.getCode()); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e(TAG, error.getMessage(), error); } }); requestQueue.add(jsonObjectRequest); } }); } }
输出的log日志如下:
三、使用lmageRequest加载图片代码如下:
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); ImageRequest imageRequest = new ImageRequest("https://cdn.pixabay.com/photo/2021/12/13/15/14/flowers-6868494_960_720.jpg", new Response.Listener<Bitmap>() { @Override public void onResponse(Bitmap response) { imageView.setImageBitmap(response); } }, 0, 0, ImageView.ScaleType.FIT_XY,Bitmap.Config.RGB_565,new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { imageView.setImageResource(R.drawable.fan1); } }); requestQueue.add(imageRequest);
四、使用lmageLoader加载图片代码如下:
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() { @Nullable @Override public Bitmap getBitmap(String url) { return null; } @Override public void putBitmap(String url, Bitmap bitmap) { } }); //第二个参数:默认先加载的图片。 //第三个参数:没有网络,或者加载错误的情况下加载的图片。 ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.drawable.fan1, R.drawable.ic_launcher); imageLoader.get("https://cdn.pixabay.com/photo/2021/12/13/15/14/flowers-6868494_960_720.jpg", listener, 0, 0);
四、使用NetworklmageView加载图片
1、首先在布局文件中添加NetworkImageView控件
<com.android.volley.toolbox.NetworkImageView android:id="@+id/networkImageView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" />
在VolleyActivity中具体实现:
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() { @Nullable @Override public Bitmap getBitmap(String url) { return null; } @Override public void putBitmap(String url, Bitmap bitmap) { } }); networkImageView.setDefaultImageResId(R.drawable.fan1); networkImageView.setErrorImageResId(R.drawable.ic_launcher); networkImageView.setImageUrl("https://cdn.pixabay.com/photo/2021/12/13/15/14/flowers-6868494_960_720.jpg", imageLoader);