Android使用Volley獲取json數(shù)據(jù)

原文鏈接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' //用于序列化

創(chuàng)建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)
    }
}

創(chuàng)建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的的哥參數(shù):
Request.Method.GET : 表示是獲取數(shù)據(jù)
"https://...." : 地址
Response.Listener :獲取成功響應
Response.ErroeListener :獲取失敗響應

源碼地址:GIthub && CSDN

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末磁滚,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子宵晚,更是在濱河造成了極大的恐慌垂攘,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件淤刃,死亡現(xiàn)場離奇詭異晒他,居然都是意外死亡,警方通過查閱死者的電腦和手機逸贾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門陨仅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來津滞,“玉大人,你說我怎么就攤上這事灼伤〈バ欤” “怎么了?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵狐赡,是天一觀的道長撞鹉。 經(jīng)常有香客問我,道長颖侄,這世上最難降的妖魔是什么鸟雏? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮览祖,結果婚禮上孝鹊,老公的妹妹穿的比我還像新娘。我一直安慰自己展蒂,他們只是感情好又活,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著锰悼,像睡著了一般皇钞。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上松捉,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天,我揣著相機與錄音馆里,去河邊找鬼隘世。 笑死,一個胖子當著我的面吹牛鸠踪,可吹牛的內容都是我干的丙者。 我是一名探鬼主播,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼营密,長吁一口氣:“原來是場噩夢啊……” “哼械媒!你這毒婦竟也來了?” 一聲冷哼從身側響起评汰,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤纷捞,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后被去,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體主儡,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年惨缆,在試婚紗的時候發(fā)現(xiàn)自己被綠了糜值。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片丰捷。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖寂汇,靈堂內的尸體忽然破棺而出病往,到底是詐尸還是另有隱情,我是刑警寧澤骄瓣,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布停巷,位于F島的核電站,受9級特大地震影響累贤,放射性物質發(fā)生泄漏叠穆。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一臼膏、第九天 我趴在偏房一處隱蔽的房頂上張望硼被。 院中可真熱鬧,春花似錦渗磅、人聲如沸嚷硫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽仔掸。三九已至,卻和暖如春医清,著一層夾襖步出監(jiān)牢的瞬間起暮,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工会烙, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留负懦,地道東北人。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓柏腻,卻偏偏與公主長得像纸厉,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子五嫂,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內容

  • 一:Volley簡介 我們平時在開發(fā)Android應用的時候不可避免地都需要用到網(wǎng)絡技術颗品,而多數(shù)情況下應用程...
    心中客閱讀 462評論 0 0
  • Volley簡介 Volley 是 Google I/O 2013上發(fā)布的網(wǎng)絡通信庫,使網(wǎng)絡通信更快沃缘、更簡單躯枢、更健...
    shenhuniurou閱讀 925評論 0 3
  • 本系列主要記錄學習android開發(fā)網(wǎng)絡請求和圖片加載框架的使用。 網(wǎng)絡操作時官方一般都會介紹HttpClient...
    Zaker2Magic閱讀 1,027評論 0 2
  • Volley框架 Volley是Google官方出的一套小而巧的異步請求庫槐臀,該框架封裝的擴展性很強闺金,支持HttpC...
    void_Zhao閱讀 10,702評論 2 2
  • 窗外的雨聲還在悠哉、悠哉的折騰著峰档。今晚的這場雨之后沒有見到什么彩虹败匹。窗外寨昙,雨簾蒙蒙;窗內掀亩,心亦重重 這樣的心情一點...
    90后青衣閱讀 579評論 0 2