你是新来的android我试图使用jsonarray请求将json响应传递给arraylist但我收到此错误:
错误:
凌空错误org.json.JSONException:值< java.lang.String类型的br无法转换为JSONArray
Php代码:
$userid=$_POST["userid"];
$semester=$_POST["semester"];
$level=$_POST["level"];
$stmt="SELECT coursecode,coursetitle,grade,credithrs,marks FROM tblresults WHERE userid = '$userid' and semester = '$semester' and level = '$level'";
$result=mysqli_query($conn,$stmt);
$responsea=array();
while($respons=mysqli_fetch_array($result)){
$response["coursecode"]=$respons['coursecode'];
$response["coursetitle"]=$respons['coursetitle'];
$response["grade"]=$respons['grade'];
$response["credithrs"]=$respons['credithrs'];
$response["marks"]=$respons['marks'];
$responsea[]=$response;
}
echo json_encode($responsea);
Php回应:
[{"coursecode":"csc234","coursetitle":"Information Security","grade":"A","credithrs":"3","marks":"80.00"},{"coursecode":"csc300","coursetitle":"Cryptography","grade":"B","credithrs":"3","marks":"65.00"}]
android代码:
public ArrayList<resultInstance> extractresult() {
CheckResultFragment checkResultFragment=new CheckResultFragment();
final String gsem=checkResultFragment.gsem;
final String glev=checkResultFragment.glev;
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, jsonurl, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
int i=0;
while (i<response.length()){
try {
JSONObject jsonObject=response.getJSONObject(i);
resultInstance resultinstance =new resultInstance(
jsonObject.getString("coursecode"),
jsonObject.getString("coursetitle"),
jsonObject.getInt("credit"),
jsonObject.getDouble("marks"),
jsonObject.getString("grade"));
results.add(resultinstance);
i++;
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params=new HashMap<>();
params.put("userid",guid);
params.put("level",glev);
params.put("semester",gsem);
return params;
}
};
VolleySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);
return results;
}
任何帮助将不胜感激!!
答案 0 :(得分:0)
调试和设置断点,你的API调用php响应不是JSONArray的格式。发送适当的JSONArray格式作为对API调用请求的响应。