Volley StringRequest传递一个整数

时间:2019-10-14 00:06:39

标签: kotlin android-volley

我正在尝试调用API,并且必须将Integer作为参数传递。问题在于,paramether必须是字符串。如何获得作为整数的值?

问题出在params.put

val stringRequest = object : StringRequest(Request.Method.GET, URL, Response.Listener<String>{ s ->
        try {
            val array = JSONArray(s)
            for (i in 0..array.length() - 1) {
                val objectComment = array.getJSONObject(i)
                val comment= Comment(
                    objectComment.getInt("userId"),
                    objectComment.getInt("id"),
                    objectComment.getString("title"),
                    objectComment.getString("body"))
                    list.add(comment)
            }
        }catch (e: JSONException){
            e.printStackTrace()
        }
    }, Response.ErrorListener { error: VolleyError? -> Log.e("error", error.toString())  }){
        override fun getParams(): Map<String, String> {
            val params = HashMap<String, String>()
            params.put("postId", 2)
            return params
        }
    }

0 个答案:

没有答案