【问题标题】:Can't get the HTTPS response using Volley无法使用 Volley 获得 HTTPS 响应
【发布时间】:2021-08-19 06:39:43
【问题描述】:

我一直在尝试了解 Volley,因此我制作了一个简单的应用程序,它有一个连接按钮,当按下按钮时,它会将响应显示为 TOAST,但是当我按下按钮时,我不能同时显示 TOAST 消息(响应 TOAST 和错误 TOAST)

这是 kotlin 代码:

import android.icu.lang.UCharacter.GraphemeClusterBreak.L
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley

class MainActivity : AppCompatActivity() {

    private val url = "https://run.mocky.io/v3/f2721dfa-df49-4c27-b1a3-4929b6b75de1"
    private var RQ  : RequestQueue? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button = findViewById<Button>(R.id.con_buttom)
        button.setOnClickListener {sendrequest()}


    }

    fun sendrequest()
    {
        val stringRequest = StringRequest(
            Request.Method.GET, url,
            {response ->  Onresponse(response)},
            {Toast.makeText(applicationContext, "Couldn't Connect", Toast.LENGTH_SHORT).show()})

        RQ?.add(stringRequest)
    }

    fun Onresponse(response : String)
    {
        Toast.makeText(getApplicationContext(),"Response :" + response.toString(), Toast.LENGTH_LONG).show()
    }

}

这是 xml 代码:

<?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">


    <Button
        android:id="@+id/con_buttom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Connect"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是网址链接:https://run.mocky.io/v3/f2721dfa-df49-4c27-b1a3-4929b6b75de1

请帮助我。我只是一个初学者 提前致谢:)

【问题讨论】:

    标签: android xml kotlin android-volley


    【解决方案1】:

    添加

     RQ = Volley.newRequestQueue(this)
    

    之后

     setContentView(R.layout.activity_main)
    

    在您的 onCreate 方法中。

        override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        RQ = Volley.newRequestQueue(this)
        val button = findViewById<Button>(R.id.con_buttom)
        button.setOnClickListener { sendrequest() }
    }
    

    因为您的 RQ 为空,所以它不起作用

    【讨论】:

    • 非常感谢,但我有疑问。据我了解,请求队列有助于将某些请求存储在缓存中,以便检索请求变得更快..即使我将它初始化为 NULL 它也不应该有任何区别,只是它会变慢,因为我们没有存储请求队列中的请求...
    • 不,RequestQueue 的唯一职责不仅仅是缓存,缓存是其中的一部分,它的主要职责是执行您创建的请求。如果它为空,则根本不会处理您的请求。如果你想了解更多关于 volley 缓存的信息,请阅读 developer.android.com/training/volley/requestqueue
    猜你喜欢
    • 2017-08-26
    • 2015-11-18
    • 1970-01-01
    • 2016-11-05
    • 2014-06-06
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多