原文链接https://www.shanya.world/archives/5428a106.html
首先例子用的json文件是我和该Demo例子一起上传至GitHub的一个json文件:地址如下
https://raw.githubusercontent.com/Shanyaliux/VolleyDemo/master/app/jsonfile/volleydemo.json
其内容我瞎编的
{
"name": "tom",
"chinese": 64,
"math": 92,
"english": 84
}
添加权限
在AndroidManifest.xml
文件添加图片中红框内容
添加Volley依赖
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.google.code.gson:gson:2.8.6' //用于序列化
创建VolleySingleton类
保证只存在一个Volley实例
package com.shanya.volleydemo
import android.content.Context
import com.android.volley.RequestQueue
import com.android.volley.toolbox.Volley
class VolleySingleton private constructor(context: Context){
companion object{
private var INSTANCE : VolleySingleton ?= null
fun getInstance(context: Context) = run {
INSTANCE?: synchronized(this){
VolleySingleton(context).also { INSTANCE = it }
}
}
}
val requestQueue:RequestQueue by lazy {
Volley.newRequestQueue(context.applicationContext)
}
}
创建data class用于json的解析
package com.shanya.volleydemo
data class Student (
val name:String,
val chinese:Int,
val math:Int,
val english:Int
)
搭建简单的显示界面
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:text="name:"
android:textSize="24sp"
app:layout_constraintEnd_toStartOf="@+id/nameTextView"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="@+id/textView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chinese:"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/chineseTextView"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.416" />
<TextView
android:id="@+id/chineseTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="@+id/textView3" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="268dp"
android:text="math:"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/mathTextView"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/mathTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="@+id/textView5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toTopOf="@+id/textView5" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="English:"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="@+id/englishTextView"
app:layout_constraintEnd_toStartOf="@+id/englishTextView"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/englishTextView"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/englishTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="140dp"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textView7" />
</androidx.constraintlayout.widget.ConstraintLayout>
显示效果
在这里插入图片描述
主要代码MainActivity
package com.shanya.volleydemo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
val stringRequest = StringRequest(
Request.Method.GET,
"https://raw.githubusercontent.com/Shanyaliux/VolleyDemo/master/app/jsonfile/volleydemo.json",
Response.Listener {
nameTextView.text = Gson().fromJson(it,Student::class.java).name
chineseTextView.text = Gson().fromJson(it,Student::class.java).chinese.toString()
mathTextView.text = Gson().fromJson(it,Student::class.java).math.toString()
englishTextView.text = Gson().fromJson(it,Student::class.java).english.toString()
},
Response.ErrorListener {
Log.d("VolleyErrorListener",it.toString())
}
)
VolleySingleton.getInstance(application).requestQueue.add(stringRequest)
}
}
}
StringRequest的的哥参数:
Request.Method.GET : 表示是获取数据
"https://...." : 地址
Response.Listener :获取成功响应
Response.ErroeListener :获取失败响应
网友评论