Android Volley 基本使用

Android Volley 基本使用

本篇主要介紹 Google 給Android 平臺提供的 Volley 一個 Http請求庫 , 齊射!

1.概述

Volley是Google 提供的一個小巧的異步請求庫,擴展很強支持okhttp,(默認是 Android2.3 及以上基於 HttpURLConnection,2.3 以下基於 HttpClient 實現), Volley 英文齊射的意思 就是指無數急促的請求,適合數據量小,並且通信頻繁的場景

官方文檔 https://google.github.io/volley/

image-20221223143616856

2.準備工作

想通過 volley 調用一個我自己的博客文章接口 然後展示標題 和 短描述 到 頁面上

2.1 編寫佈局文件

上面展示標題 下面展示 短描述

image-20221224011336283

<?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/showTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textStyle="bold"
        android:textSize="25sp"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.2" />

    <TextView
        android:id="@+id/showShortDesc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/showTitle"
        app:layout_constraintVertical_bias="0.2" />


</androidx.constraintlayout.widget.ConstraintLayout>

2.2 提供博客接口地址

#隨便找了我的一篇博客的 請求地址
https://www.askajohnny.com/blogs/blogInfo/303/15

2.3 通過JSON To Kotlin Class 插件生成 data class (kotlin)

和 IDEA 中 Json Formatter插件類似,它是生成JAVA的, 而這個 JSON To Kotlin Class 插件是用來生成kotlin 的數據類的

右擊文件 Generate.. 或者 control + 回車 喚起轉化窗口

image-20221223173306949

package com.johnny.volleydemo

data class BlogInfo(
    val code: Int,
    val `data`: Data,
    val msg: String
)

data class Data(
    val anchors: List<Anchor>,
    val blogContent: String,
    val blogImageUrl: String,
    val blogMdContent: String,
    val blogShortContent: String,
    val blogTitle: String,
    val blogTypeAnchor: Any,
    val blogTypeId: String,
    val blogTypeName: Any,
    val clickCount: Int,
    val createDate: String,
    val createMonth: Any,
    val createTime: String,
    val createUser: Any,
    val id: Int,
    val isThumbed: String,
    val nextBlogId: Any,
    val nextBlogTitle: Any,
    val previousBlogId: Any,
    val previousBlogTitle: Any,
    val thumbCount: Int,
    val updateTime: String
)

data class Anchor(
    val anchorId: String,
    val anchorName: String
)

3.引入依賴

根據你的dsl 語言 選擇適合的方式引入依賴

Groovy

dependencies {
    implementation 'com.android.volley:volley:1.2.1'
}

Kotlin

dependencies {
    implementation("com.android.volley:volley:1.2.1")
}

4.發送請求

使用 volley 需要先構建一個請求, 並且把請求提交到 newRequestQueue 隊列中, 提交後 volley 會根據構建的請求異步發送請求, 只需要在回調的地方處理請求的響應即可

4.1 StringRequest 構建請求

volley 提供了 StringRequest 構建請求

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val titleTextView = findViewById<TextView>(R.id.showTitle)
        val shortDescTextView = findViewById<TextView>(R.id.showShortDesc)

        //使用 volley需要創建一個Queue
        val requestQueue = Volley.newRequestQueue(this)
        //請求的 博客 url
        val url =
            "https://www.askajohnny.com/blogs/blogInfo/303/15"
        //構建StringRequest請求
        val stringRequest = StringRequest(Request.Method.GET,
            url, {
                //由於我後端沒有在header 的 charset中返回 UTF-8 所以默認當做ISO-8859-1格式
                //所以這裏需要先轉化成 UTF-8
                val data = String(
                    it.toByteArray(Charsets.ISO_8859_1),
                    Charsets.UTF_8
                )
                Log.d(TAG, "onCreate: stringRequest  ${data}")
                //通過Gson轉化 成上面生成的 數據類
                val blogInfo = Gson().fromJson(data, BlogInfo::class.java)
                //把後端返回的數據 展示在 textview上
                titleTextView.text = blogInfo.data.blogTitle
                shortDescTextView.text = blogInfo.data.blogShortContent
            }, {
                Log.d(TAG, "onCreate: stringRequest error ${it.message}")
            })
        //把請求推入 queue 會自動進行異步請求
        requestQueue.add(stringRequest)
    }

效果如下..

image-20221224012424698

4.2 JsonObjectRequest 構建請求

按照 JSONObject 獲取數據

第二個參數 null 表示Get請求 第二個參數如果有設置 則是post方式

GET 請求

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val titleTextView = findViewById<TextView>(R.id.showTitle)
        val shortDescTextView = findViewById<TextView>(R.id.showShortDesc)

        //val url = "https://weather.api.bdymkt.com/day?city=無錫"
        //
        val requestQueue = Volley.newRequestQueue(this)
        val url =
            "https://www.askajohnny.com/blogs/blogInfo/303/15"
        val postRequest = object : JsonObjectRequest(url, null,
            {
                Log.d(TAG, "volleyInitData: request success $it")
                titleTextView.text = it.getJSONObject("data").get("blogTitle") as String?
                shortDescTextView.text = it.getJSONObject("data").get("blogShortContent") as String
            }, {
                Log.d(TAG, "volleyInitData: request error ${it.message}")
            }) {
            override fun getHeaders(): MutableMap<String, String> {
                val headers = mutableMapOf<String, String>()
                headers["Accept"] = "application/json"
                headers["Content-Type"] = "application/json; charset=UTF-8"
                return headers
            }
        }
        requestQueue.add(postRequest)
    }

POST請求

此時 第二個參數設置了JSONObject() 是 post方式

        val jsonObject = JSONObject()
        jsonObject.put("pageNumber", 0)
        jsonObject.put("pageSize", 20)
        val jsonArray = JSONArray()
        jsonObject.put("ids", jsonArray)
				//此時 第二個參數設置了 是 post方式 
        val postRequest = object : JsonObjectRequest(requestUrl, jsonObject, {
            Log.d(TAG, "volleyInitData: jsonstr:$it")
            val jsonStr = it.toString()
            val blogInfo = Gson().fromJson(jsonStr, BlogInfo::class.java)
            blogAdapter.list
                .addAll(blogInfo.data.content)
            blogAdapter.notifyDataSetChanged()
        }, {
            Log.d(TAG, "volleyInitData: request error ${it.message}")
        }) {
            override fun getHeaders(): MutableMap<String, String> {
                val headers = mutableMapOf<String, String>()
                headers["Accept"] = "application/json";
                headers["Content-Type"] = "application/json; charset=UTF-8";
                return headers
            }
        }

5. 擴展

5.1 添加 Header 和 Params

注意 需要object: 進行匿名內部類, 重寫 getHeaders getParams getPriority 等等方法

//注意 需要object: 進行匿名內部類,  重寫 getHeaders  getParams 方法
val stringRequest = object : StringRequest(Request.Method.GET,
    url, {
        val data = String(
            it.toByteArray(Charsets.ISO_8859_1),
            Charsets.UTF_8
        )
        Log.d(TAG, "onCreate: stringRequest  ${data}")
        val blogInfo = Gson().fromJson(data, BlogInfo::class.java)
        titleTextView.text = blogInfo.data.blogTitle
        shortDescTextView.text = blogInfo.data.blogShortContent
    }, {
        Log.d(TAG, "onCreate: stringRequest error ${it.message}")
    }) { //最後面 大括號 裏面是匿名內部類重新方法
    override fun getHeaders(): MutableMap<String, String> {
        //返回 map map裏面添加 需要放入Header的數據
        return super.getHeaders()
    }

    override fun getParams(): MutableMap<String, String>? {
        //返回 map map裏面添加 需要添加的 query params
        return super.getParams()
    }
     //指定 優先級
     override fun getPriority(): Priority {
        return Priority.HIGH
     }
}

5.2 取消隊列中的請求

如果想把隊列中的請求取消 , 需要給請求設置一個 tag , 然後調用隊列的 cancelAll 可以把指定tag的請求取消了

 //...

 stringRequest.setTag("obj");
 queue.add(objRequest);
        //取消請求
 queue.cancelAll("obj");

總結

本篇主要介紹 andriod 中 Volley的基本使用方式,它是官方開發的一個HTTP框架 簡化操作 , Volley 的設計目標就是非常適合去進行數據量不大,但通信頻繁的網絡操作,而對於大數據量的網絡操作,比如說下載文件等,Volley的表現就會非常糟糕

亂碼問題參考:

https://blog.csdn.net/yangbiyao/article/details/51270839

歡迎大家訪問 個人博客 Johnny小屋
歡迎關注個人公衆號

歡迎關注個人公衆號

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章