我有一个php页面(urldisplay)返回这个问题对象数组:
[{
"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"
}]
我试图通过这个类(在我的DAO类中)读取对象,将结果保存到数组的问题列表中
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;
}
}
任务保持空白。然后我想将问题对象添加到ListView。
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);
}
我尝试过其他一些SO帖子,但我似乎在追逐我的尾巴。任何人都可以发现我的错误/我有更好的方法吗?
干杯,
答案 0 :(得分:1)
问题是AsyncTask执行代码Asunchrounsly,这意味着当你调用ListSugg()
时,它将在另一个线程上运行后台任务,并在后台任务完成之前返回任务对象,这意味着你&#39;我还有一个空列表。 read here for more info
你可以做的是,你覆盖onPostExecute
方法,并在那里设置适配器,如下所示:
public class ListSuggestions extends AsyncTask<Void, Void, Void> {
@Override
protected Boolean doInBackground(String... params) {
// do something
return true;
}
@Override
protected void onPostExecute(String result) {
// The results of the above method
// Processing the results here
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
list);
listv.setAdapter(arrayAdapter);
}
}
但要实现这一点,您需要在MainActivity中包含AsyncTask。
这应解决您的问题,希望有所帮助。
但是,有更好更简单的解决方案,比如使用像okhttp或Retrofit这样的HTTP网络库,并使用像Gson这样的JSON解析器,如果你愿意,可以查看它们。
答案 1 :(得分:0)
启动后台任务后,您将返回ques
。后台任务需要一些时间才能完成,在此之前,您的ques
列表将保持为空,因此您将看不到数据。
更新AsyncTask的postExecute
方法中的列表。
答案 2 :(得分:0)
试试这个
quest = new ArrayList<Question>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Question ques = new Question(); //inti Question
//Enter Values to Question Object
ques.setQuestion( jsonObject.getString("question"));
ques.setAnswer1( jsonObject.getString("answer1"));
ques.setAnswer2( jsonObject.getString("answer2"));
ques.setAnswer3( jsonObject.getString("answer3"));
ques.setAnswer4( jsonObject.getString("answer4"));
ques.setCorrectAnswer( jsonObject.getString("correctanswer"));
// Add Question Object to ArrayList
quest.add(ques);
}
答案 3 :(得分:0)
它是空的,因为你从doInBackground()返回null。理想的方法是将结果传递给onPostExecute()并在那里设置适配器。就像这样: -
public class ListSuggestions extends AsyncTask<Void, Void, ArrayList<Question>> {
@Override
protected ArrayList<Question> 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 quest;
}
@Override
protected void onPostExecute(ArrayList<Question> questions) {
super.onPostExecute(questions);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
list );
listv.setAdapter(arrayAdapter);
}
}