Hi, I have php, which json'om returns to me several values:
- day
- class
- letter
- lesson
- task
It takes them from the SQL table and returns sort of like an array.
Here is the php code:
$sql = "SELECT * FROM `table_name`"; $result = $conn->query($sql); while ($row = $result->fetch_assoc()) { $day["day:"] = ($row['day']); $class["class:"] = ($row['class']); $letter["letter:"] = ($row['letter']); $lesson["lesson:"] = ($row['lesson']); $task["task:"] = ($row['task']); echo json_encode($day); echo json_encode($class); echo json_encode($letter); echo json_encode($lesson); echo json_encode($task); } So I try to accept it in java code, and I seem to accept it, and everything would be okay, but then I encounter a problem ..
I need to stuff the data into a ListView ..
This script gives me such a thing:
{"day:":"Четверг"}{"class:":"3 класс"}{"letter:":"А"}{"lesson:":"География"}{"task:":"Задание 1"} I took it in the application to the string and passed it to the JSONObject and try to pass to the ListView with the following code:
public void JSONURL(String result) { try { JSONObject json = new JSONObject(result); System.out.println(json); JSONObject uDay = json.getJSONObject("day"); JSONObject uClass = json.getJSONObject("class"); JSONObject uLetter = json.getJSONObject("letter"); JSONObject uLesson = json.getJSONObject("lesson"); JSONObject uTask = json.getJSONObject("task"); for (int i = 0; i < uLesson.length(); i++) { HashMap<String, Object> hm; hm = new HashMap<String, Object>(); hm.put(DAY, uDay.getString("day").toString()); hm.put(CLASS, uClass.getString("class").toString()); hm.put(LETTER, uLetter.getString("letter").toString()); hm.put(LESSON, uLesson.getString("lesson").toString()); hm.put(TASK, uTask.getString("task").toString()); myBooks.add(hm); SimpleAdapter adapter = new SimpleAdapter(showHomework.this, myBooks, R.layout.list, new String[] { TASK, CLASS, LETTER, LESSON, DAY}, new int[] { R.id.text1, R.id.text2, R.id.text3, R.id.text4, R.id.text5 }); //выводим в листвью listView.setAdapter(adapter); listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); } } catch (JSONException e) { Log.e("log_tag", "Error parsing data " + e.toString()); } } Vooot ... And the Android Studio terminal gives an error:
Error parsing data org.json.JSONException: No value for day.
As I understand my code can not find the corresponding value of the day in json'e ..
Guys, please do not swear, I'm new to this thread .. Help than you can ..
day:', but you try to get theday(without a colon). You or from the array itself, in php, remove the colons from the keys, or get it right - Bookin