我正在尝试获取请求并在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? = ""
}
答案 0 :(得分:0)
您没有做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
})