protected String doInBackground(String... args) { // Будет хранить параметры List<NameValuePair> params = new ArrayList<>(); // получаем JSON строк с URL JSONObject json = jParser.makeHttpRequest("тут ссылка на сайт", "GET", params); try { // Получаем масив из Продуктов dolzno = json.getJSONArray("dolzno"); // перебор всех продуктов for (int i = 0; i < dolzno.length(); i++) { JSONObject c = dolzno.getJSONObject(i); // Сохраняем каждый json елемент в переменную String id = c.getString(TAG_ID); // Создаем новый HashMap HashMap<String, String> map = new HashMap<String, String>(); // добавляем каждый елемент в HashMap ключ => значение map.put(TAG_ID, id); // добавляем HashList в ArrayList productsList.add(map); } } catch (JSONException e) { e.printStackTrace(); } return null; } 

The json itself is a valid answer like:

{"dolzno": [{"_ id": "1"}, {"_ id": "2"}, {"_ id": "4"}], "spec": [{"_ id": "1" }, {"_ id": "2"}, {"_ id": "3"}], "otdel": [{"_ id": "1"}, {"_ id": "2"}, {" _id ":" 3 "}]}

It is obtained from the url.

As I understand it, it contains 3 objects - dolzno , otdel , spec . So I want to cram each of these objects into an arraylist . However, the program for some reason does not see these objects. (I enter them by name, in the example dolzno - the name of the first object)

After the conversion, I planned arraylist tie the arraylist to the spinner.
What is wrong? Php code below

 <?php $res = array(); $response = array(); $response1 = array(); $response2 = array(); // include db connect class require_once __DIR__ . '/db_connect.php'; // connecting to db $db = new DB_CONNECT(); // get all products from products table $result = mysql_query("SELECT * FROM dolz"); $result1 = mysql_query("SELECT * FROM otdel"); $result2 = mysql_query("SELECT * FROM spec"); if (mysql_num_rows($result) > 0 and mysql_num_rows($result1) > 0) { $response["dolzno"] = array(); $response1["otdel"] = array(); $response2["spec"] = array(); while ($row = mysql_fetch_array($result)) { $product = array(); $product["_id"] = $row["_id"]; array_push($response["dolzno"], $product); $res=array_merge((array)$res, (array)$response); } while ($row = mysql_fetch_array($result2)) { $product2 = array(); $product2["_id"] = $row["_id"]; array_push($response2["spec"], $product2); $res=array_merge((array)$res, (array)$response2); } while ($row = mysql_fetch_array($result1)) { $product1 = array(); $product1["_id"] = $row["_id"]; array_push($response1["otdel"], $product1); $res=array_merge((array)$res, (array)$response1); } echo json_encode($res); } else { $response1["success"] = 0; $response1["message"] = "No products found"; echo json_encode($response1); } ?> 
  • A very simple example: stackoverflow.com/questions/312660/… - abbath0767
  • HashMap in the loop is clearly superfluous, push the lines directly into the productsList, and everything else is OK. What is the actual problem? - Eugene Krivenja
  • The program does not see json at all - element111

1 answer 1

In general, the problem was on the side of the php code. Everything is solved (although I myself do not understand why) by adding a minor code. Below is the working code:

 <?php $res = array(); $response = array(); $response1 = array(); $response2 = array(); // include db connect class require_once __DIR__ . '/db_connect.php'; // connecting to db $db = new DB_CONNECT(); // get all products from products table $result = mysql_query("SELECT * FROM dolz"); $result1 = mysql_query("SELECT * FROM otdel"); $result2 = mysql_query("SELECT * FROM spec"); if (mysql_num_rows($result) > 0 and mysql_num_rows($result1) > 0) { $response["dolzno"] = array(); while ($row = mysql_fetch_array($result)) { $product = array(); $product["_id"] = $row["_id"]; array_push($response["dolzno"], $product); $res=array_merge((array)$res, (array)$response); } $response1["otdel"] = array(); while ($row = mysql_fetch_array($result1)) { $product1 = array(); $product1["_id"] = $row["_id"]; array_push($response1["otdel"], $product1); $res=array_merge((array)$res, (array)$response1); } $response2["spec"] = array(); while ($row = mysql_fetch_array($result2)) { $product2 = array(); $product2["_id"] = $row["_id"]; array_push($response2["spec"], $product2); $res=array_merge((array)$res, (array)$response2); } $res["success"] = 1; echo json_encode($res); } else { $res["success"] = 0; $res["message"] = "No products found"; echo json_encode($res); } ?>