在我的代码中,我试图从MySQL数据库中填充值微调器,但是我无法获得该值。
我的jsonArray出了什么问题?
java.lang.String无法转换为JSONObject
JsonArrayRequest jsonSpinnerObjectRequest = new JsonArrayRequest(fetch_city_url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
JSONObject jsonObject = null;
JSONArray jsonArray = jsonObject.getJSONArray("result");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject city = jsonArray.getJSONObject(i);
Log.i("onResponseSpinner", city.getString("kd_kelas") + " " + city.getString("kelas"));
kelasList.add(city.getString("kelas"));
}
spinnerAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY","ERROR in response"+error.getMessage());
}
});
答案 0 :(得分:0)
在这里,您将JSON对象设置为null,然后尝试从中获取JSON数组:
JSONObject jsonObject = null;
JSONArray jsonArray = jsonObject.getJSONArray("result");
我认为你想要这样的东西:
public void onResponse(JSONObject response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray = jsonArray = new jsonObject.getJSONArray("result");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject city = jsonArray.getJSONObject(i);
Log.i("onResponseSpinner", city.getString("kd_kelas") + " " + city.getString("kelas"));
kelasList.add(city.getString("kelas"));
}
spinnerAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
答案 1 :(得分:0)
查看您的代码,我认为您已经在响应中获取JSON数组,因此只需从此JSON数组中获取值,请查看以下代码
JsonArrayRequest jsonSpinnerObjectRequest = new JsonArrayRequest(fetch_city_url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject city = response.getJSONObject(i);
Log.i("onResponseSpinner", city.getString("kd_kelas") + " " + city.getString("kelas"));
kelasList.add(city.getString("kelas"));
}
spinnerAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY","ERROR in response"+error.getMessage());
}
});