Ich habe eine PHP-Seite (urldisplay) dieses Array von Frage Objekten Rückkehr:Fetch JSON Array Arraylist Listview
[{
"suggestid": "1",
"question": "What does Aotearoa mean?",
"answer1": "Maui and the Whale",
"answer2": "2 Islands",
"answer3": "Land of the Long White Cloud",
"answer4": "Isle of Greenstone",
"correctanswer": "Land of the Long White Cloud",
"userID": "1"
}, {
"suggestid": "2",
"question": "What does Haere Ra mean?",
"answer1": "Hello",
"answer2": "Goodbye",
"answer3": "Thanks",
"answer4": "Sorry",
"correctanswer": "Goodbye",
"userID": "1"
}, {
"suggestid": "3",
"question": "Where is Zealand?",
"answer1": "France",
"answer2": "Germany",
"answer3": "Denamrk",
"answer4": "Finland",
"correctanswer": "Denmark",
"userID": "1"
}, {
"suggestid": "4",
"question": "What does Aotearoa mean?",
"answer1": "Maui and the Whale",
"answer2": "2 Islands",
"answer3": "Land of the Long White Cloud",
"answer4": "Isle of Greenstone",
"correctanswer": "Land of the Long White Cloud",
"userID": "2"
}, {
"suggestid": "5",
"question": "What does Aotearoa mean?",
"answer1": "Maui and the Whale",
"answer2": "2 Islands",
"answer3": "Land of the Long White Cloud",
"answer4": "Isle of Greenstone",
"correctanswer": "Land of the Long White Cloud",
"userID": "3"
}]
Ich versuche, die Objekte über diese Klasse zu lesen (in meiner DAO-Klasse), die spart die Ergebnisse zu einem Array Liste von Fragen
ArrayList<Question> quest;
public ArrayList<Question> ListSugg()
{
ListSuggestions sugg = (ListSuggestions)new ListSuggestions().execute();
return quest;
}
public class ListSuggestions extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL(urldisplay);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
JSONArray jsonArray = new JSONArray(con);
quest = new ArrayList<Question>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String question = jsonObject.getString("question");
String answer1 = jsonObject.getString("answer1");
String answer2 = jsonObject.getString("answer2");
String answer3 = jsonObject.getString("answer3");
String answer4 = jsonObject.getString("answer4");
String correctanswer = jsonObject.getString("correctanswer");
Question ques = new Question(0, question, answer1, answer2, answer3, answer4, correctanswer);
quest.add(ques);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e1) {
e1.printStackTrace();
}
return null;
}
}
Quest bleibt leer, obwohl. Ich möchte dann die Frageobjekte zu einer ListView hinzufügen.
public class ListActivity extends AppCompatActivity {
ListView listv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listv = (ListView)findViewById(R.id.ListDisplay);
DAO dao = new DAO();
ArrayList list = dao.ListSugg();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
list);
listv.setAdapter(arrayAdapter);
}
Ich habe ein paar andere SO-Posts ausprobiert, aber ich scheine meinen Schwanz zu verfolgen. Kann jemand meine Fehler erkennen/Gibt es einen besseren Weg für mich, dies zu tun?
Cheers,
Was ist der Fehler Sie konfrontiert sind? –
Fragen, die Debugging-Hilfe suchen ("Warum funktioniert dieser Code nicht?") Müssen das gewünschte Verhalten, ein bestimmtes Problem oder einen Fehler und den kürzesten Code enthalten, der für die Reproduktion in der Frage erforderlich ist. Fragen ohne eine klare Problemstellung sind für andere Leser nicht nützlich. Siehe: [Wie man ein minimales, vollständiges und überprüfbares Beispiel erstellt.] (Https://stackoverflow.com/help/mcve) – LazerBanana
Das jsonarray ist leer, also schlägt es bei der for-Schleife fehl – Gremio