无法使用 Volley 获取 HTTPS 响应
Can't get the HTTPS response using Volley
我一直在努力学习 Volley,因此我制作了一个简单的应用程序,它有一个连接按钮,当按下按钮时,它会将响应显示为 TOAST,但是当我按下按钮时,我不能TOAST 消息(响应 TOAST 和错误 TOAST)
这是科特林代码:
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>
这里是 URL link : https://run.mocky.io/v3/f2721dfa-df49-4c27-b1a3-4929b6b75de1
请帮助我。我只是个初学者
提前致谢:)
添加
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 是空的
我一直在努力学习 Volley,因此我制作了一个简单的应用程序,它有一个连接按钮,当按下按钮时,它会将响应显示为 TOAST,但是当我按下按钮时,我不能TOAST 消息(响应 TOAST 和错误 TOAST)
这是科特林代码:
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>
这里是 URL link : https://run.mocky.io/v3/f2721dfa-df49-4c27-b1a3-4929b6b75de1
请帮助我。我只是个初学者 提前致谢:)
添加
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 是空的