Java 如何显示问题和选项?
我正在开发一个Android应用程序,它需要显示一个问题(TextView)和四个可能的答案(带单选按钮的RadioGroup)。我使用Web服务器和MySql将数据库连接到我的应用程序中。为了获取数据,我在PHP中使用此方法将数据转换为JSONJava 如何显示问题和选项?,java,android,json,Java,Android,Json,我正在开发一个Android应用程序,它需要显示一个问题(TextView)和四个可能的答案(带单选按钮的RadioGroup)。我使用Web服务器和MySql将数据库连接到我的应用程序中。为了获取数据,我在PHP中使用此方法将数据转换为JSON <?php include_once 'db.php'; class Quiz{ private $db; private $db_table = "aenglish_questions"; public function __
<?php
include_once 'db.php';
class Quiz{
private $db;
private $db_table = "aenglish_questions";
public function __construct(){
$this->db = new DbConnect();
}
public function getAllQuizQuestions(){
$json_array = array();
$query = "select * from aenglish_questions";
$result = mysqli_query($this->db->getDb(), $query);
if(mysqli_num_rows($result) > 0){
while ($row = mysqli_fetch_assoc($result)) {
$json_array["quiz_questions"][] = $row;
}
}
return $json_array;
}
}
?>
如果您提出问题并添加所收到错误的堆栈跟踪,可能会有所帮助。您好,Abra爵士,我编辑了我的问题并发布了我收到的错误消息。请调试并检查您的回答是否包含数据引发异常的代码行是文件
Question10Fragment.java
中的第164行。看起来您调用了类ArrayList
的方法get()
,但该列表为空。你需要调试你的代码,并试图发现为什么你的列表是空的。感谢各位的回复,我已经解决了我的问题,你们是最好的,直到下次。
private class AsyncJsonObject extends AsyncTask<String, Void, String> {
private ProgressDialog progressDialog;
@Override
protected String doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httpPost = new HttpPost("http://192.168.1.11/LETIA/includes/index.php");
String jsonResult = "";
try {
HttpResponse response = httpClient.execute(httpPost);
jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
System.out.println("Returned Json object " + jsonResult.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonResult;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog = ProgressDialog.show(getActivity(), "Downloading Quiz","Wait....", true);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
System.out.println("Resulted Value: " + result);
parsedObject = returnParsedJsonObject(result);
if(parsedObject == null){
return;
}
quizCount = parsedObject.size();
firstQuestion = parsedObject.get(0);
quizQuestion.setText(firstQuestion.getQuestion());
String[] possibleAnswers = firstQuestion.getAnswers().split(",");
optionOne.setText(possibleAnswers[0]);
optionTwo.setText(possibleAnswers[1]);
optionThree.setText(possibleAnswers[2]);
optionFour.setText(possibleAnswers[3]);
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = br.readLine()) != null) {
answer.append(rLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return answer;
}
}
private List<QuizWrapper> returnParsedJsonObject(String result){
List<QuizWrapper> jsonObject = new ArrayList<QuizWrapper>();
JSONObject resultObject = null;
JSONArray jsonArray = null;
QuizWrapper newItemObject = null;
try {
resultObject = new JSONObject(result);
System.out.println("Testing the water " + resultObject.toString());
jsonArray = resultObject.optJSONArray("aenglish_questions");
} catch (JSONException e) {
e.printStackTrace();
}
for(int i = 0; i < jsonArray.length(); i++){
JSONObject jsonChildNode = null;
try {
jsonChildNode = jsonArray.getJSONObject(i);
int id = jsonChildNode.getInt("qae_id");
String question = jsonChildNode.getString("question");
String answerOptions = jsonChildNode.getString("choices");
int correctAnswer = jsonChildNode.getInt("answer");
newItemObject = new QuizWrapper(id, question, answerOptions, correctAnswer);
jsonObject.add(newItemObject);
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonObject;
}
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pre_boardreviewer, PID: 26290
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:437)
at com.example.recall.EnglishFragments.Question10Fragment$AsyncJsonObject.onPostExecute(Question10Fragment.java:164)
at com.example.recall.EnglishFragments.Question10Fragment$AsyncJsonObject.onPostExecute(Question10Fragment.java:123)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.access$600(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7156)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)