[英]Android: Send a simple request with Volley not working
我认为这是一个愚蠢的问题,但我真的不知道我的简单应用程序出了什么问题(顺便说一句,我是 Android 新手)。
即使这个“简单”的例子对我也不起作用(这令人沮丧!)
https://developer.android.com/training/volley/simple
我总是收到消息“那不起作用!”。
我的代码是。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textView = findViewById<TextView>(R.id.text)
val queue = Volley.newRequestQueue(this)
val url = "http://www.apple.com"
// Request a string response from the provided URL.
val stringRequest = StringRequest(Request.Method.GET, url, Response.Listener<String> { response ->
// Display the first 500 characters of the response string.
textView.text = "Response is: ${response.substring(0, 500)}"
},
Response.ErrorListener { textView.text = "That didn't work!" })
// Add the request to the RequestQueue.
queue.add(stringRequest)
}
}
我在 Mac Book 上使用 Android Studio 和虚拟设备进行开发。
有什么建议么?
看起来您的代码没问题,所以您的应用可能缺少 Internet 权限,这对于 HTTP 请求当然是强制性的。
请将此添加到您的AndroidManifest.xml
中:
<uses-permission android:name="android.permission.INTERNET"/>
我知道这已经很晚了,但我认为,在我尝试使用 HTTPS 而不是 HTTP 之前,我遇到了同样的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.