Volley,ngrok-无法使用Volley向ngrok REST API发出发布请求

标记

我一直在将ngrok用于大量项目,主要是语音应用程序和rest api。我只需要在Android应用中使用rest api。但是,无论我做什么,由于某些原因它都行不通!

如果我尝试使用http,这是我得到的错误:

E/Volley: [17118] BasicNetwork.performRequest: Unexpected response code 307 for http://1z5d90b4.ngrok.io/api/v1/users/id/
I/System.out: That didn't work! com.android.volley.ServerError 

如果我尝试https链接,这就是我得到的:


com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: SSL handshake aborted: ssl=0xd7821100: I/O error during system call, Connection reset by peer

我不知道为什么,怎么回事!我在计算机上检查了ngrok runnign,但它什么也没收到。仅供参考,我正在Kotlin中使用Volley对该URL发出发布请求,这是我的代码:

        val queue = Volley.newRequestQueue(this)

    // Request a string response from the provided URL.
        val stringRequest = JsonObjectRequest(Request.Method.POST, apiUrl, params,
                Response.Listener { response ->
                    // Display the first 500 characters of the response string.
                    println("Response is: $response")
                },
                Response.ErrorListener { error ->  println("That didn't work! ${error}") })
        queue.add(stringRequest)

标记

我发现了问题!听起来很傻,但是那是网络防火墙一旦我切换了网络,一切正常!!!

仅添加一些内容,如果HTTP请求有问题,请尝试添加android:usesCleartextTraffic="true"到AndroidManifest.xml文件。将会是这样的:

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>

    <!--Add this line if you haven't added it yet-->
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        ...

       <!--ALSO, this can fix some of the problems-->
        android:usesCleartextTraffic="true"


        ...>
        ...
    </application>
</manifest>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章