Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android kotlin-从url获取简单字符串_Android_Kotlin_Android Volley - Fatal编程技术网

Android kotlin-从url获取简单字符串

Android kotlin-从url获取简单字符串,android,kotlin,android-volley,Android,Kotlin,Android Volley,我真不敢相信没有人回答 如何从https://ipinfo.io/ip哪一个只不过是一个简单的字符串 这就是我尝试过的: var soo = "meh" val queue = Volley.newRequestQueue(this) val stringRequest = StringRequest(Request.Method.GET, "https://ipinfo.io/ip", object : Response.Listener<

我真不敢相信没有人回答

如何从
https://ipinfo.io/ip
哪一个只不过是一个简单的字符串

这就是我尝试过的:

    var soo = "meh"

    val queue = Volley.newRequestQueue(this)
    val stringRequest = StringRequest(Request.Method.GET, "https://ipinfo.io/ip",
            object : Response.Listener<String> {
                override fun onResponse(response: String) {
                    // Display the first 500 characters of the response string
                    soo = response
                    Log.d("letsSee", soo) // THIS WAS CALLED SECOND: the ip
                }
            }, object : Response.ErrorListener {
        override fun onErrorResponse(error: VolleyError) {
            soo = "error occurred"
        }
    })
    queue.add(stringRequest)

    Log.d("letsSee", soo) // THIS WAS CALLED FIRST: "meh"
var soo=“meh”
val queue=Volley.newRequestQueue(此)
val stringRequest=stringRequest(Request.Method.GET)https://ipinfo.io/ip",
对象:Response.Listener{
重写响应(响应:字符串){
//显示响应字符串的前500个字符
soo=响应
Log.d(“letsSee”,soo)//这被称为第二个:ip
}
},对象:Response.ErrorListener{
覆盖错误响应(错误:截击错误){
soo=“发生错误”
}
})
添加(stringRequest)
Log.d(“letsSee”,soo)//这被称为第一个:“meh”
这正在工作

    var soo = "meh"

    val queue = Volley.newRequestQueue(this)
    val stringRequest = StringRequest(Request.Method.GET, "https://ipinfo.io/ip",
            com.android.volley.Response.Listener<String> { response ->
                soo = response
                Log.d("see again", soo)
            }, com.android.volley.Response.ErrorListener {
        // didn't work
    });
    queue.add(stringRequest)

    Log.d("letsSee", soo)
var soo=“meh”
val queue=Volley.newRequestQueue(此)
val stringRequest=stringRequest(Request.Method.GET)https://ipinfo.io/ip",
com.android.volley.Response.Listener{Response->
soo=响应
Log.d(“再次看到”,soo)
},com.android.volley.Response.ErrorListener{
//没用
});
添加(stringRequest)
日志d(“letsSee”,soo)

在android中,所有网络调用都是异步的,不会在主线程上执行, Volley遵循相同的方法,使其网络调用异步, 所以在您的代码“Log.d(“letsSee”,soo)”中,语句不会等待Volley执行网络调用,而是执行它

所以你必须像这样创建回调接口

interface ApiResponse{
fun onSuccess(response:String)
fun onError()
fun getMyIp(apiResponse: ApiResponse) {
    val queue = Volley.newRequestQueue(this)
    val url = "https://ipinfo.io/ip"

    val stringRequest = StringRequest(Request.Method.GET, url,
            Response.Listener<String> { response ->
                apiResponse.onSuccess(response)
            },
            Response.ErrorListener {
                apiResponse.onError()
            }
    )

    queue.add(stringRequest)
}
getMyIp(object :ApiResponse{
        override fun onSuccess(response: String) {
            Log.d("SSB Log", response)
        }

        override fun onError() {
            Log.d("SSB Log", "Error")
        }

    })
}

然后做一个这样的函数

interface ApiResponse{
fun onSuccess(response:String)
fun onError()
fun getMyIp(apiResponse: ApiResponse) {
    val queue = Volley.newRequestQueue(this)
    val url = "https://ipinfo.io/ip"

    val stringRequest = StringRequest(Request.Method.GET, url,
            Response.Listener<String> { response ->
                apiResponse.onSuccess(response)
            },
            Response.ErrorListener {
                apiResponse.onError()
            }
    )

    queue.add(stringRequest)
}
getMyIp(object :ApiResponse{
        override fun onSuccess(response: String) {
            Log.d("SSB Log", response)
        }

        override fun onError() {
            Log.d("SSB Log", "Error")
        }

    })

您还可以在类级别实现ApiResponse接口

也许
onResponse(…)
没有被调用?更新
onErrorResponse(..)
中的
soo=“error accurrent”
,查看请求是否失败。PS:您是否添加了internet权限?不,既不调用onResponse也不调用onErrorResponse,我认为这是因为“队列”,但如何解决这个问题?我还有一个合适的方法从url获取字符串,而不是最后一个日志语句:
log.d(“letsSee”,soo)//这是第一个调用的:“meh”
将始终首先调用并打印“meh”,这很好,因为您正在执行一个在单独线程上运行的异步HTTP请求。因此,只要请求被放入队列,您的应用程序就不会等待应答,而是继续执行,以满足
Log
语句并打印
meh
,因为变量
soo
尚未更新。如果您必须对IP结果执行任何操作,比如在
文本视图中显示它,那么它必须从
onResponse(…)
执行这就是我的想法,这很明显,但是如何解决这个问题呢?我需要它是非异步的不,如果你尝试执行非异步http请求,你的应用程序将崩溃,出现
NetworkOnMainThreadException
。只需执行
onResponse(…)
中的任何逻辑,或者执行从
onResponse(…)
调用的单独方法。未解决的引用:body。我也希望看到一个完整的另一种方式,因为这一种方式看起来很奇怪耶,我在你提问的同时更新了我的答案,你在错误的时间调用了日志不,这与以前完全相同,botton的日志在方法响应之前被称为B耶,我保留底部日志只是为了测试目的,当然,您可以忽略它或删除它,带有“再次查看”标签的一个可以纠正系统限制和好处,就像硬币的两面一样,因此当我们是软件开发人员时,我们必须遵循它们:)