简体   繁体   中英

Android: Send a simple request with Volley not working

I think this is a stupid question, but I really do not know what went wrong with my simple app (I am a Android newbie btw).

Even this "easy" example is not working for me (thats frustrating!)

https://developer.android.com/training/volley/simple

I always get the message "That didn't work!".

My code is.

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)

    }

}

I develop with Android Studio and a Virtual Device on a Mac Book.

Any suggestions?

Looks like your code is OK so maybe your app is missing Internet permissions, which is of course mandatory for HTTP requests.

Please add this to your AndroidManifest.xml :

<uses-permission android:name="android.permission.INTERNET"/>

我知道这已经很晚了,但我认为,在我尝试使用 HTTPS 而不是 HTTP 之前,我遇到了同样的问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM