简体   繁体   English

Android:在 Volley 不工作的情况下发送一个简单的请求

[英]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).我认为这是一个愚蠢的问题,但我真的不知道我的简单应用程序出了什么问题(顺便说一句,我是 Android 新手)。

Even this "easy" example is not working for me (thats frustrating!)即使这个“简单”的例子对我也不起作用(这令人沮丧!)

https://developer.android.com/training/volley/simple 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.我在 Mac Book 上使用 Android Studio 和虚拟设备进行开发。

Any suggestions?有什么建议么?

Looks like your code is OK so maybe your app is missing Internet permissions, which is of course mandatory for HTTP requests.看起来您的代码没问题,所以您的应用可能缺少 Internet 权限,这对于 HTTP 请求当然是强制性的。

Please add this to your AndroidManifest.xml :请将此添加到您的AndroidManifest.xml中:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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