In this android volley tutorial, we’ll be implementing the Volley library in our application. If you aren’t aware of the features of Volley, we recommend you to read this article before proceeding ahead.
在此android volley教程中,我们将在应用程序中实现Volley库。 如果你不知道的抽射功能,我们建议您阅读此继续前进之前的文章。
Android Volley (Android Volley)
Network Requests in a Volley are added to a RequestQueue
. Instead of creating a new instance of RequestQueue every time, we follow the singleton pattern by creating a single instance of RequestQueue throughout our application.
将Volley中的网络请求添加到RequestQueue
。 并非每次都创建一个RequestQueue的新实例,而是通过在整个应用程序中创建单个RequestQueue的实例来遵循单例模式 。
In the application below, we’ll be implementing GET and POST StringRequest
and JsonObjectRequests
. We’ll use Gson library to parse the response and display the results in a RecyclerView.
在下面的应用程序中,我们将实现GET和POST StringRequest
和JsonObjectRequests
。 我们将使用Gson库解析响应并将结果显示在RecyclerView中。
Furthermore, we’ll implement a CustomRequest and load images from a URL in the ImageView.
此外,我们将实现CustomRequest并从ImageView中的URL加载图像。
Android Volley教程项目结构 (Android Volley Tutorial Project Structure)
Android Volley Gradle依赖关系 (Android Volley Gradle Dependencies)
Before we get onto the coding aspect of this tutorial, add the following dependencies in your build.gradle
file.
在开始学习本教程的编码之前,请在build.gradle
文件中添加以下依赖build.gradle
。
compile 'com.android.support:cardview-v7:26.1.0'
compile 'com.android.support:recyclerview-v7:26.1.0'
compile 'com.android.support:design:26.1.0'
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.android.volley:volley:1.0.0'
Android Volley示例代码 (Android Volley Example Code)
Layout – activity_main.xml
The code for the activity_main.xml
layout that holds the UI for the MainActivity.java
is given below.
布局– activity_main.xml
下面给出了activity_main.xml
布局的代码,该布局保存MainActivity.java
的UI。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.journaldev.volley.RecyclerViewActivity">
<Button
android:id="@+id/btnImageLoader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/networkImageView"
android:layout_alignStart="@+id/networkImageView"
android:layout_below="@+id/networkImageView"
android:layout_marginTop="16dp"
android:text="LOAD IMAGE" />
<Button
android:id="@+id/btnImageRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnImageLoader"
android:layout_alignBottom="@+id/btnImageLoader"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toEndOf="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:text="IMAGE REQUEST" />
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/networkImageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_below="@+id/imageView"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="8dp" />
<Button
android:id="@+id/btnGET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/btnPOST"
android:layout_alignLeft="@+id/btnPOST"
android:layout_alignRight="@+id/btnPOST"
android:layout_alignStart="@+id/btnPOST"
android:layout_centerVertical="true"
android:text="GET String JSON" />
<Button
android:id="@+id/btnPOST"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnGET"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:text="POST String JSON" />
<Button
android:id="@+id/btnCustomRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/btnPOST"
android:layout_alignLeft="@+id/btnPOST"
android:layout_alignRight="@+id/btnPOST"
android:layout_alignStart="@+id/btnPOST"
android:layout_below="@+id/btnPOST"
android:layout_marginTop="8dp"
android:text="CUSTOM REQUEST" />
</RelativeLayout>
SingletonRequestQueue.java
SingletonRequestQueue.java
package com.journaldev.volley;
import android.content.Context;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class SingletonRequestQueue {
private static SingletonRequestQueue mInstance;
private Context mContext;
private RequestQueue mRequestQueue;
private SingletonRequestQueue(Context context) {
mContext = context;
mRequestQueue = getRequestQueue();
}
public static synchronized SingletonRequestQueue getInstance(Context context) {
if (mInstance == null) {
mInstance = new SingletonRequestQueue(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(mContext);
}
return mRequestQueue;
}
}
getInstance()
and getRequestQueue()
methods create an instance of SingletonRequestQueue and RequestQueue respectively for the first time and re-use it everywhere.
getInstance()
和getRequestQueue()
方法分别首次创建SingletonRequestQueue和RequestQueue的实例,并在各处重复使用。
Linking the layout to the MainActivity.java
将布局链接到MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btnGET, btnPOST, btnImageLoader, btnCustomRequest, btnImageRequest;
NetworkImageView networkImageView;
ImageView imageView;
ArrayList<UserList.UserDataList> mUserDataList = new ArrayList<>();
String BASE_URL = "https://reqres.in";
String IMAGE_URL = "https://www.android.com/static/2016/img/share/oreo-lg.jpg";
int numberOfRequestsCompleted;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGET = findViewById(R.id.btnGET);
btnPOST = findViewById(R.id.btnPOST);
btnImageLoader = findViewById(R.id.btnImageLoader);
btnImageRequest = findViewById(R.id.btnImageRequest);
btnCustomRequest = findViewById(R.id.btnCustomRequest);
networkImageView = findViewById(R.id.networkImageView);
imageView = findViewById(R.id.imageView);
btnGET.setOnClickListener(this);
btnPOST.setOnClickListener(this);
btnImageLoader.setOnClickListener(this);
btnCustomRequest.setOnClickListener(this);
btnImageRequest.set