【发布时间】:2018-01-24 06:44:34
【问题描述】:
我有一个 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 帖子,但我似乎在追赶我的尾巴。谁能发现我的错误/我有更好的方法吗?
干杯,
【问题讨论】:
-
您遇到了什么错误?
-
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。见:How to create a Minimal, Complete, and Verifiable example.
-
jsonarray 为空,因此在 for 循环中失败
标签: java php android json listview