Does not display values ​​from the database on the server

protected String doInBackground(Void... voids) { try { URL url = new URL(json_url); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); InputStream inputStream = httpURLConnection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder stringBuilder = new StringBuilder(); while ((JSON_STRING = bufferedReader.readLine())!=null) { if(JSON_STRING != null){ try{ final JSONArray jArray = new JSONArray(JSON_STRING); for(int i=0;i<jArray.length();i++) { JSONObject json_data = jArray.getJSONObject(i); KEY_NAME_EVENT = json_data.getString("KEY_NAME_EVENT"); KEY_INFO_EVENT =json_data.getString("KEY_INFO_EVENT"); KEY_CONTACT =json_data.getString("KEY_CONTACT"); KEY_ADRESS = json_data.getString("KEY_ADRESS"); } }catch(JSONException e1){ e1.printStackTrace(); } } stringBuilder.append(KEY_ADRESS); } bufferedReader.close(); inputStream.close(); httpURLConnection.disconnect(); return stringBuilder.toString().trim(); }catch (MalformedURLException e){ e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } 

type org.json.JSONArray cannot be converted to JSONObject

  • Please indicate the line on which the error occurs. Additional useful information will be the value JSON_STRING at the moment when an error occurs. - Geslot
  • Just for analysis, screw the GSON (google JSON converter), feed it the problem variable and output it to the console. What structure will the resulting object have? Or just output at every step toString () for the object. Look, can JSONArray be returned to you, with which you need to work somehow differently? For example it will have to disassemble. - DimXenon pm
  • JSONObject json_data = jArray.getJSONObject (i); error in this line - NikitaER

2 answers 2

type org.json.JSONArray cannot be converted to JSONObject You do not have an object there, but an array

  • JSONObject json_data = jArray.getJSONObject (i); here is a mistake - NikitaER
  • You get an object from an array, and judging by the text of the error, your array consists of arrays, not objects. - Nikola Tesla

Problem solved! The problem was in the php file accessed by Json, or rather in one line: echo json_encode (array ("server_response" => $ response)); changed to: echo json_encode ($ response);