【问题标题】:How to correctly do array request in volley如何在 volley 中正确执行数组请求
【发布时间】:2019-03-04 07:00:34
【问题描述】:

我正在尝试获取请求并在 json 中获取数组,但我遇到了这样的错误:

org.json.JSONException: Value [] of type org.json.JSONArray cannot be converted to JSONObject

这是我的请求代码:

private fun getStudents(endLink: String) {
    val request = JSONObject()
    val studentLink = "https://192.168.1.1/getStudents.php?idEntity="
    val linkFull = studentLink.plus(endLink)
    val jsArrayRequest = JsonObjectRequest(Request.Method.GET, linkFull, request, Response.Listener<JSONObject> {
        val builder = GsonBuilder()
        val gson = builder.create()
        val student =
            gson.fromJson<Students>(it.toString(), students::class.java!!)
        studentResponse = studentResponse
        //updateInfo()
    }, Response.ErrorListener {
        Log.d("ERRORKA", it.message)
        Toast.makeText(
            this.context,
            it.message, Toast.LENGTH_SHORT
        ).show()
    })

    MySingleTon.getInstance(this.context!!).addToRequestQue(jsArrayRequest)
}

还有我的数据模型:

class StudentResponse {
var groupNumber: String = ""
var students: List<Students>? = null
}

这是第二个:

class Students {
val id: Int = 0
val firstName: String? = ""
val lastName: String? = ""
val middleName: String? = ""
val email: String? = ""
}

【问题讨论】:

    标签: kotlin android-volley


    【解决方案1】:

    你不是在做jsonArrayRequest,你可以看到你正在创建的对象是JsonObjectRequest。像下面那样做。

        // Method: POST
        val mDataArray = JSONArray(); // This should be JSONArray object
        val mRequest = JsonArrayRequest(Request.Method.POST,"YOUR API URL", mDataArray,{
            //Response listener
        },{
            //Error listener
        })
    
        // Method: GET
        val mRequest = JsonArrayRequest(Request.Method.GET,"YOUR API URL", null,{
            //Response listener
        },{
            //Error listener
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-21
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多