[英]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.