Android Quiz Application with Json Parser, Php and Mysql Database
In this tutorial we are going to learn how to create an android quiz application with Json parser, Php and Mysql database. This is going to be a long tutorial so relax as we take the journey together.
Most of the concepts you will see here has been covered in some of my previous tutorials and I will suggest you read these two tutorials before you continue – How to Parse JSON in Android and Android Login and Registration with Php and Mysql.
We are going to store our quiz questions and answers in Mysql database which can be local or remote. One advantage of using this concept in android quiz application is that it will afford you the opportunity to add and edit quiz questions and solutions with the need to update your application.
Android quiz application can also be achieved by storing the quiz questions and solutions using android SQLITE database. In this case, the application will not require internet connection to access the database.
If you want a complete Android Quiz App with Web Admin Panel follow this link
If you do not know much about Php and Mysql, kindly read it up w3school.com.
Before we start with the detailed instruction, it is important to know that we will first of all download and install a web server in our system. I am going to use WAMP server but feel free to use whatever web server you are familiar with.
The detail instruction on how to install WAMP can be found on their website.
With the web server installed, start your web server and go to PhpMyAdmin. We will create a new database called quiz. In our created database, we will create a single table called quiz_questions. The below script is used to create our users table.
CREATE TABLE IF NOT EXISTS 'quiz_questions' ( 'id' bigint(20) NOT NULL AUTO_INCREMENT, 'question' varchar(300) NOT NULL, 'possible_answers' varchar(300) NOT NULL, 'correct_answer' int(5) NOT NULL, PRIMARY KEY ('id'), UNIQUE KEY 'question' ('question') ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The next step is to create our Php project structure. I will make use of Aptana IDE for Php and Mysql development but feel free to use whatever IDE you like the best.
Create a folder called Android_Quiz inside the www folder of your web server. Inside this folder, create another folder called include. The include folder will house our configuration file for database connection.
Create a file called index.php in your project root directory. All the request that comes to the web server will go through this index file.
In our include folder, create three files called config.php, db.php and quiz.php.
When a client sends a request, the index.php file will analyze the request and talk to the database. It will retrieve all the quiz questions present in the quiz_questions table. The data will first be converted to an array and rather will be output as a Json object.
The android application will consume the Json object and we will use android Json parsing API to manipulate the content of the server response.
Open the Php database config file called config.php and paste the following code on it.
<?php define("DB_HOST", "localhost"); define("DB_USER", "quiz"); define("DB_PASSWORD", "quiz"); define("DB_NAME", "quiz"); ?>
This is the database connection details. Please make sure you change this information in accordance with your database server. In the code above we are just defining the database connection properties.
With this in place, we will establish a database connection with our Php application. This is done in the db.php file. Open the file and paste the following code below.
<?php include_once 'config.php'; class DbConnect{ private $connect; public function __construct(){ $this->connect = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); if (mysqli_connect_errno($this->connect)){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); } } public function getDb(){ return $this->connect; } }
The config file was included in the beginning of db.php file. The getDB function returns an instance of our database connection.
Now that we have our database connection, create a quiz.php file. The quiz class contains a function that will query the database to obtain all the quiz questions present in the database.
The quiz class includes the db.php class. Copy and paste the following code in the file.
<?php include_once 'db.php'; class Quiz{ private $db; private $db_table = "quiz_questions"; public function __construct(){ $this->db = new DbConnect(); } public function getAllQuizQuestions(){ $json_array = array(); $query = "select * from quiz_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; } } ?>
Now we will finish up with our index.php file. We will include the quiz.php file in the index.php. What it will basically do is to return all the quiz questions store in MYSQL database and output it as a Json object.
Please note that this is a simple tutorial and there is no other measure taken to make sure that the request is not malicious.
The code is below. You can copy it and paste in your own project.
<?php require_once 'include/quiz.php'; $quizObject = new Quiz(); $json_quiz_objects = $quizObject->getAllQuizQuestions(); echo json_encode($json_quiz_objects); ?>
A simple example of the Json object output from the server is shown below
{"quiz_questions":[{"id":"1","question":"Who is the most popular musician in Nigeria","possible_answers":"Tufac, P Square, Dbanji, Tiwa Savage","correct_answer":"1"},{"id":"2","question":"Who is the winner of 2015 Presidential Election in Nigeria?","possible_answers":"Obasanjo, Goodluck, Buhari, Fashola","correct_answer":"3"}]}
With the web application completed, we will move over to create our android application.
In order to parse JSON object from a remote server source, we are going to need internet access for our application.
It is important to note that when you are working with internet or accessing data from a remote source in android, you will need to add internet permission in your android manifest file.
<uses-permission android:name="android.permission.INTERNET"/>
We are going to use android API for HTTP request. Once we get the Json output as an InputStream, we will convert it to a string. The string object will be pass as a parameter to an instance of JsonObject class.
We will also use an AsyncTask class to make server calls in a background Thread. This is important so that we will not end up blocking the main UI Thread.
Before we start, the first thing I will do is to list the environment and tools I used in this android tutorial but feel free to use whatever environment or tools you are familiar with.
Windows 7
Android Studio
Samsung Galaxy Fame Lite
Min SDK 14
Target SDK 19
To create a new android application project, following the steps as stipulated below.
Go to File menu
Click on New menu
Click on Android Application
Enter Project name: AndroidQuizApplication
Package: com.inducesmile.androidquizapplication
Keep other default selections.
Continue to click on next button until Finish button is active, then click on Finish Button
Once you are done with creating your project, make sure you change the package name if you did not use the same package.
Quiz Design
The first step here is to have a clear view about what we are planning to achieve. In this android quiz application, there will be an intro activity page with a button when a user clicks on the button view it will take the user to the quiz activity page where the user will start taking the quiz. A diagram that illustrate the quiz application is shown below.
Main Layout
We will start by adding background image to the root layout in our activity_main.xml layout file. The image I used can be found in the project source code below. You can use your own image be changing the above used image.
Add a Button View to the layout as shown in the image above. If you are using Eclipse or Android Studio, you can switch to the design view and drag and drop this View inside your layout file.
You can also copy and paste the following code below into this file if you don’t want to do it yourself.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginRight="32dp" android:layout_marginEnd="32dp" android:background="@drawable/mbutton" android:layout_marginBottom="48dp" /> </RelativeLayout>
The text content of our View components are stored in the strings.xml file. The updated contain of the file is shown below
<resources> <string name="app_name">Android Quiz Application</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="title_activity_quiz">Quiz Questions</string> <string name="question">Question 1. How is the first president of Nigeria?</string> <string name="answer_one">King Ade</string> <string name="answer_two"> olu Obasanjo</string> <string name="answer_three">Jona Goodluck</string> <string name="answer_four"> Atiku Abubakar</string> <string name="next_questions"> Next Question</string> <string name="previous_questions"> Previous Question</string> </resources>
We will make use of few colors in this application so we have to update our color.xml file to include all these colours. This is important because it lets us have a single place where we can change and set colours for our project. The code is shown below.
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="black">#000000</color> <color name="white">#ffffff</color> </resources>
MainActivity.java
In the MainActivity.java file, we will get the instance of our button View and attach and event listener to the button View. When the button is click, it will redirect to the QuizActivity page. The code for the MainActivity.java is shown below.
package inducesmile.com.androidquizapplication; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button startButton = (Button)findViewById(R.id.button); startButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, QuizActivity.class); startActivity(intent); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Quiz Page Layout
We will proceed to the activity_quiz.xml file to create our quiz interface. The quiz interface will contain the following View components
1. TextView – this is hold the quiz questions
2. RadioGroup – A wrapper for RadioButton Views
3. 4 RadioButtons – This will serve as answer options for the quiz.php
4. 2 Buttons – previous and next buttons will be used to change the quiz questions
The following is the code for the layout. You can copy and paste it in your own project.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="inducesmile.com.androidquizapplication.QuizActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/question" android:id="@+id/quiz_question" android:layout_alignParentTop="true" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginEnd="10dp" android:layout_marginStart="10dp" android:layout_marginTop="20dp" android:textSize="20sp" android:textColor="#000000" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/quiz_question" android:layout_alignLeft="@+id/quiz_question" android:layout_alignStart="@+id/quiz_question" android:layout_marginTop="40dp" android:id="@+id/radioGroup"> <RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/radio0" android:textSize="15sp" android:textColor="#000000" android:text="@string/app_name" android:layout_marginBottom="10dp" android:paddingLeft="20dp" android:button="@drawable/radio_bg"/> <RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/radio1" android:textSize="15sp" android:textColor="@color/black" android:text="@string/app_name" android:layout_marginBottom="10dp" android:paddingLeft="20dp" android:button="@drawable/radio_bg"/> <RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/radio2" android:textSize="15sp" android:textColor="@color/black" android:text="@string/app_name" android:layout_marginBottom="10dp" android:paddingLeft="20dp" android:button="@drawable/radio_bg"/> <RadioButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/radio3" android:textSize="15sp" android:textColor="@color/black" android:text="@string/app_name" android:paddingLeft="20dp" android:button="@drawable/radio_bg"/> </RadioGroup> <Button android:layout_height="wrap_content" android:layout_width="160dp" android:gravity="center" android:id="@+id/nextquiz" android:textColor="@color/white" android:text="@string/next_questions" android:background="@drawable/quizbutton" android:layout_marginRight="10dp" android:padding="5dp" android:layout_alignParentRight="true" android:layout_alignBaseline="@+id/previousquiz"/> <Button android:layout_height="wrap_content" android:layout_width="160dp" android:gravity="center" android:id="@+id/previousquiz" android:textColor="@color/white" android:text="@string/previous_questions" android:background="@drawable/quizbutton" android:layout_below="@+id/radioGroup" android:layout_alignLeft="@+id/radioGroup" android:padding="5dp" android:layout_marginTop="20dp" android:layout_alignStart="@+id/radioGroup" /> </RelativeLayout>
We will finally look at the code for the QuizActivity class before we conclude this tutorial.
QuizActivity
The code in the class is huge so I will try as much as possible to explain what each block is doing. But like in MainActivity.java class, we are going to get all the instances of our View controls in QuizActivity.java.
We created an AsyncTask class as an inner class to our Activity class. The AsyncTask class is used to make request to the server since it has its own separate thread and each request call will be interfere with the main UI thread.
In the AsyncTask doInBackground method, the request to the server is sent and server response are passed as a parameter to the onPostExecute method of the class.
A progressbar is used to keep the app on hold until the questions are download from the server.
Once the quiz questions are download, the data is parsed using android built-in Json object and array parser classes. The return data are passed into a domain object each is then stored in a List.
The code of the domain class is shown below.
package inducesmile.com.androidquizapplication; public class QuizWrapper { private int id; private String question; private String answers; private int correctAnswer; public QuizWrapper(int id, String question, String answers, int correctAnswer) { this.id = id; this.question = question; this.answers = answers; this.correctAnswer = correctAnswer; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getQuestion() { return question; } public void setQuestion(String question) { this.question = question; } public String getAnswers() { return answers; } public void setAnswers(String answers) { this.answers = answers; } public int getCorrectAnswer() { return correctAnswer; } public void setCorrectAnswer(int correctAnswer) { this.correctAnswer = correctAnswer; } }
The stored data are then set to the quiz questions using question number index. The code for the QuizActivity.java is shown below.
package inducesmile.com.androidquizapplication; import android.app.ProgressDialog; import android.content.pm.ActivityInfo; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class QuizActivity extends ActionBarActivity { private TextView quizQuestion; private RadioGroup radioGroup; private RadioButton optionOne; private RadioButton optionTwo; private RadioButton optionThree; private RadioButton optionFour; private int currentQuizQuestion; private int quizCount; private QuizWrapper firstQuestion; private List<QuizWrapper> parsedObject; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_quiz); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); quizQuestion = (TextView)findViewById(R.id.quiz_question); radioGroup = (RadioGroup)findViewById(R.id.radioGroup); optionOne = (RadioButton)findViewById(R.id.radio0); optionTwo = (RadioButton)findViewById(R.id.radio1); optionThree = (RadioButton)findViewById(R.id.radio2); optionFour = (RadioButton)findViewById(R.id.radio3); Button previousButton = (Button)findViewById(R.id.previousquiz); Button nextButton = (Button)findViewById(R.id.nextquiz); AsyncJsonObject asyncObject = new AsyncJsonObject(); asyncObject.execute(""); nextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int radioSelected = radioGroup.getCheckedRadioButtonId(); int userSelection = getSelectedAnswer(radioSelected); int correctAnswerForQuestion = firstQuestion.getCorrectAnswer(); if(userSelection == correctAnswerForQuestion){ // correct answer Toast.makeText(QuizActivity.this, "You got the answer correct", Toast.LENGTH_LONG).show(); currentQuizQuestion++; if(currentQuizQuestion >= quizCount){ Toast.makeText(QuizActivity.this, "End of the Quiz Questions", Toast.LENGTH_LONG).show(); return; } else{ firstQuestion = parsedObject.get(currentQuizQuestion); quizQuestion.setText(firstQuestion.getQuestion()); String[] possibleAnswers = firstQuestion.getAnswers().split(","); uncheckedRadioButton(); optionOne.setText(possibleAnswers[0]); optionTwo.setText(possibleAnswers[1]); optionThree.setText(possibleAnswers[2]); optionFour.setText(possibleAnswers[3]); } } else{ // failed question Toast.makeText(QuizActivity.this, "You chose the wrong answer", Toast.LENGTH_LONG).show(); return; } } }); previousButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { currentQuizQuestion--; if(currentQuizQuestion < 0){ return; } uncheckedRadioButton(); firstQuestion = parsedObject.get(currentQuizQuestion); 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]); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_quiz, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } 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://10.0.2.2/androidlogin/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(QuizActivity.this, "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("quiz_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("id"); String question = jsonChildNode.getString("question"); String answerOptions = jsonChildNode.getString("possible_answers"); int correctAnswer = jsonChildNode.getInt("correct_answer"); newItemObject = new QuizWrapper(id, question, answerOptions, correctAnswer); jsonObject.add(newItemObject); } catch (JSONException e) { e.printStackTrace(); } } return jsonObject; } private int getSelectedAnswer(int radioSelected){ int answerSelected = 0; if(radioSelected == R.id.radio0){ answerSelected = 1; } if(radioSelected == R.id.radio1){ answerSelected = 2; } if(radioSelected == R.id.radio2){ answerSelected = 3; } if(radioSelected == R.id.radio3){ answerSelected = 4; } return answerSelected; } private void uncheckedRadioButton(){ optionOne.setChecked(false); optionTwo.setChecked(false); optionThree.setChecked(false); optionFour.setChecked(false); } }
You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.
Remember to subscribe with your email so that you will be among the first to receive our new post once it is published.
when running the project it gives this runtime error:
05-13 21:18:04.340 1768-1768/inducesmile.com.androidquizapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: inducesmile.com.androidquizapplication, PID: 1768
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at inducesmile.com.androidquizapplication.QuizActivity$AsyncJsonObject.onPostExecute(QuizActivity.java:189)
at inducesmile.com.androidquizapplication.QuizActivity$AsyncJsonObject.onPostExecute(QuizActivity.java:146)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
It seems your server response is empty or null. Please make sure that the quiz questions are returned from your database server.
Lease how to add a question and answer at php my admin :'( i am confus
If you are using a local server like WAMP, go to Myphpadmin, find your database. Add new questions to the quiz table using the insert interface.
If you have a live server, you can login to your database server and manual add the questions to the quiz table.
Another option is to write a php script that will insert data to your database.
I hope it helps. Do let me know if you still have any problem.
Hi
The application shows up on the AVD. When i press the start quiz. It says downloading quiz. And then the application stops stating AndroidQuiz application is note responding.
10-19 09:09:04.834 3042-3042/inducesmile.com.androidquizapplication W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:651)
10-19 09:09:04.834 3042-3042/inducesmile.com.androidquizapplication W/System.err: at android.os.AsyncTask.-wrap1(AsyncTask.java)
10-19 09:09:04.834 3042-3042/inducesmile.com.androidquizapplication W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication W/System.err: at android.os.Looper.loop(Looper.java:148)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication W/System.err: at java.lang.reflect.Method.invoke(Native Method)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication D/AndroidRuntime: Shutting down VM
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: FATAL EXCEPTION: main
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: Process: inducesmile.com.androidquizapplication, PID: 3042
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method ‘int org.json.JSONArray.length()’ on a null object reference
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: at inducesmile.com.androidquizapplication.QuizActivity.returnParsedJsonObject(QuizActivity.java:227)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: at inducesmile.com.androidquizapplication.QuizActivity.access$1300(QuizActivity.java:34)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: at inducesmile.com.androidquizapplication.QuizActivity$AsyncJsonObject.onPostExecute(QuizActivity.java:182)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: at inducesmile.com.androidquizapplication.QuizActivity$AsyncJsonObject.onPostExecute(QuizActivity.java:145)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: at android.os.AsyncTask.finish(AsyncTask.java:651)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: at android.os.AsyncTask.-wrap1(AsyncTask.java)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-19 09:09:04.835 3042-3042/inducesmile.com.androidquizapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-19 09:09:13.299 3042-3042/? I/Process: Sending signal. PID: 3042 SIG: 9
I don’t see any errors anywhere else.
I am using a Nexus5 AVD.
Im having the exact error as you sir. Please help me how you fix it
Hello, im having a problem, when i run the app, it stops. but this only happens when the database is ON. when i turn off my database the app displays “Downloading quiz”
Can you please make a tutorial for this one because i am beginner in Android development and programming
Sir I getting this error while running php file
Fatal error: Class ‘DbConnect’ not found in C:xampphtdocsAndroid_Quizincludequiz.php on line 13
I wrote the same code as you given by you pls help me.. Thank you.
Add include_once “DbConnect.php” at the beginning of the Quiz class. Do let me know if it works.
Hey Man, in one of your replies you mentioned ..”It seems your server response is empty or null. Please make sure that the quiz questions are returned from your database server.” well how exactly do you ensure it’s not doing that…
i have received the following error .my database have values .what is the mistake pls reply
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at inducesmile.com.androidquizapplication.QuizActivity$AsyncJsonObject.onPostExecute(QuizActivity.java:189)
at inducesmile.com.androidquizapplication.QuizActivity$AsyncJsonObject.onPostExecute(QuizActivity.java:146)
at android.os.AsyncTask.finish(AsyncTask.java:602)
at android.os.AsyncTask.access$600(AsyncTask.java:156)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Please check you response object if it contains a value. Also make sure that you are using a correct path to your web server.
the response object contain the value as shown below.then what mistake
Returned Json object {“quiz_questions”:[{“id”:”1″,”question”:”what is my first name”,”possible_answer”:”guil,raj,bert,john”,”correct_answer”:”1″},{“id”:”2″,”question”:”what is my last name”,”possible_answer”:”guil,raj,john,jose”,”correct_answer”:”2″}]}
the response object contain the value as shown below.then what mistake
Returned Json object {“quiz_questions”:[{“id”:”1″,”question”:”what is my first name”,”possible_answer”:”guil,raj,bert,john”,”correct_answer”:”1″},{“id”:”2″,”question”:”what is my last name”,”possible_answer”:”guil,raj,john,jose”,”correct_an
Can you paste the code in the line number where the error occur?
The error occur at onpostexecute method.at following line
quizCount = parsedObject.size();
firstQuestion = parsedObject.get(0);
Size return 0.so showing index is invalid.thanks for your reply.
boss getting json object, but still code is not working, crashing with error null pointer exception,
I tried returning json array but it’s not working either
——— beginning of crash
08-18 00:19:08.003 2583-2583/inducesmile.com.androidquizapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: inducesmile.com.androidquizapplication, PID: 2583
java.lang.NullPointerException: Attempt to invoke virtual method ‘int org.json.JSONArray.length()’ on a null object reference
at
HI SIR . I FOUND sOME eRROR AND THE APP IS cRUSHING
ava.lang.NullPointerException: Attempt to invoke virtual method ‘int org.json.JSONArray.length()’ on a null object reference
at. HOW CAN I FIX IT . THANK YOU
It implies that you are returning a null Json response object and your are call a method length() on a null object.
Make sure that you return a quiz object from your server
Sir
Im having a force close when I start the quiz. it say Downloading Quiz then after a seconds it will say. “Unfortunately Error occured”
Please help me.
Please tell me the error from your logcat it will help in solving your problem. Force close can occur because of too many reasons. I need to see the error message.
Sorry sir my android run slow XD
Process: com.example.joshua.myapplication, PID: 7149
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.joshua.myapplication/com.example.joshua.myapplication.QuizActivity}; have you declared this activity in your AndroidManifest.xml?
This is where I think the problem is. You app cannot find QuizActivity class in your manifest file.
So open your AndroidManifest.xml file and check if there is a reference to this activity class if not you need to add it like the way other activity class appeared in Manifest.xml file.
I have declared already sir. The only this I dont declare is the QuizWrapper. Im using API 23.
Sorry for grammar . *thing
How Im gonna Fixed it sir? please your help will be the key for my Thesis
i have used your code for creating quiz app but now i want entry of every selected option of question into new table in a database.
can you please provide code for the same.
thank you !
If you want a unique code for your project you can contact me directly.
Hello sir,…It is the best and easy tutorial.It works fine without any issues.Please make
a tutorial how to add scoring with result screen.
Hello Henry, I was working on your application and I have the same problem than JoshToGO, when I’m dowloading the Quiz, the app suddently stops, I checked out all the things you talked about before, I fill my DB with all the parameters like you do on the example and I chekout the manifest and everything is in order, what do you think can be the problem?
It is hard to know the problem without your logcat error. Please check your logcat and let me know the error you see there. It can help in fixing it.
why not use volley instead of async task?
I wrote it around 2015 but most of my recent tutorials on network calls are on Volley or Retrofit. Feel free to change yours to volley if you want to use volley. Thanks for stopping by
Can you help converting the code to volley am stuck?
public void getQuestion() {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUrl, null, new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray questions = response.getJSONArray(“questions”);
for(int i=0; i<questions.length(); i++){
JSONObject quiz_questions = questions.getJSONObject(i);
String firstQuestion = quiz_questions.getString("firstQuestion");
String optionOne = quiz_questions.getString("optionOne");
String optionTwo = quiz_questions.getString("optionTwo");
String optionThree = quiz_questions.getString("optionThree");
String optionFour = quiz_questions.getString("optionFour");
quizQuestion.append(firstQuestion+ "" +optionOne+"" +optionTwo+""+optionThree+""+optionFour+ "");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonObjectRequest);
}
you can reformat the code and use volley, why not do it, it will help new bies since asnyctask is kinda deprecated , but good work
Can you teach us on how do you do a android quiz application using volley?
android newbie here. thank you
I am writing a new tutorial on this topic. It will be soon publish here. Just stick around
Hehehe thank you sir. your tutorials is very helpful on our thesis project. keep it up. i’ll be waiting for this. thank you again.
Hello sir,
i have a problem. I can’t get the next question after clicking next button. as i have used currentQuizQuestion++; this code also.
waiting for your reply..
thank you
Does it mean that you can get only one / first question in the quiz?
When i click on next question button app gets crashed getting error ArrayIndexOutOfBoundsException ……Please help…..
Show error message. It will help. Thanks
how to update data ……….without previous data change
how to get json file with utf-8 encoding.
hey, i’ve downloaded your source and replicated the db, succesfully compiled and running in my phone (SDK23). But, everytime i pressed start quiz button, the app just close without any warning. Any clue?
Send me the error message. Thanks
Please sir send me these project php files db.php ,config.php and quiz.php im not understand in these tutorial