【问题标题】:put parameter in request volley android with Kotlin使用 Kotlin 将参数放入请求 volley android
【发布时间】:2020-10-14 14:12:24
【问题描述】:

我需要向 Volley 和 kotlin 提出请求,我的 cod 是

val stringReq = StringRequest(
Request.Method.GET, url,
Response.Listener<String> { response ->

var strResp = response.toString()
            val jsonObj: JSONObject = JSONObject(strResp)
            val jsonArray: JSONArray = jsonObj.getJSONArray("curso")

for (i in 0 until jsonArray.length()) {

                var jsonInner: JSONObject = jsonArray.getJSONObject(i)
                listGeral.add(jsonInner.get("interpret").toString());
                listGeral2.add(jsonInner.get("titel").toString());
                listGeral3.add(jsonInner.get("id").toString());

            }

},
        Response.ErrorListener {

        })
    queue.add(stringReq)

现在我需要为 php 发送 3 个参数。发送参数的指令怎么写?

【问题讨论】:

标签: android kotlin android-volley


【解决方案1】:

你可以在Response.ErrorListener后面写下面的代码

      val sr: StringRequest = object : StringRequest(Method.POST, "url",
                Response.Listener { response -> 

                     //your response
              },
                Response.ErrorListener { error -> 
                     //your error
              }) {
            override fun getParams(): Map<String, String> {
                val params: MutableMap<String, String> = HashMap()
                params["user"] = "YOUR USERNAME"
                params["pass"] = "YOUR PASSWORD"
                return params
            }

            @Throws(AuthFailureError::class)
            override fun getHeaders(): Map<String, String> {
                val params: MutableMap<String, String> = HashMap()
                params["Content-Type"] = "application/x-www-form-urlencoded"
                return params
            }
        }
        queue.add(sr)

【讨论】:

  • Javad Dehban 查找此错误图像kmshow.com.br/kotlin/error.png
  • 现在我测试你的脚本已经完成,但是当我把我的“for”放入(响应)应用程序崩溃时
  • 当我放一个循环来获取 json 时应用程序在运行时崩溃
  • 通过将我的 content-type 更改为 application/x-www-form-urlencoded 这对我有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多