title: get Jsonarray as response by android volley by sending some data
body: <p>Can someone tell me....how to get jsonarray as reponse by android volley library...I used the method called <strong>jsonArrayRequest</strong> but there is one thing....I want to get jsonArray response by sending some data to the php script.....My php script contain three variables ...and i will pass these three variables from android app....then my php script genertae the jsonarray response based on these three values....I used getParams method to send the data to the php script...but when i demand jsonArray response the android never hit the getParams method.....but when i use stringRequest......it will work properly......But i want jsonArray response not the stringRequest....</p>
  
  <p>Can someone help me how to do this??
  Thanks in advance</p>
  
  <pre><code>    package com.example.shreeram.dreamapp;
  
      import android.content.Intent;
      import android.graphics.Typeface;
      import android.support.annotation.NonNull;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.widget.ArrayAdapter;
      import android.widget.Spinner;
      import android.widget.TextView;
  
      import com.android.volley.AuthFailureError;
      import com.android.volley.Request;
      import com.android.volley.RequestQueue;
      import com.android.volley.Response;
      import com.android.volley.VolleyError;
      import com.android.volley.toolbox.JsonArrayRequest;
      import com.android.volley.toolbox.StringRequest;
      import com.android.volley.toolbox.Volley;
  
      import org.json.JSONArray;
      import org.json.JSONException;
      import org.json.JSONObject;
  
      import java.util.ArrayList;
      import java.util.Collection;
      import java.util.HashMap;
      import java.util.Map;
      import java.util.Set;
  
      public class QuestionActivity extends AppCompatActivity {
  
  
          // declare all views...
          TextView tv1,tv2,tv3,tv4,tv5;
          Spinner spinner;
  
  
          //php script url.....
          String server_url = "http://192.168.212.2/question.php";
  
  
          ArrayList&lt;String&gt; question;
          ArrayList&lt;String&gt; option1;
          ArrayList&lt;String&gt; option2;
          ArrayList&lt;String&gt; option3;
          ArrayList&lt;String&gt; option4;
          ArrayList&lt;Integer&gt; correct;
          ArrayList&lt;Integer&gt; q_id;
          ArrayList&lt;Integer&gt; id;
  
  
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_question);
  
  
              //receive data from previous activity....data is .....value of click and listclick..
              Intent in = getIntent();
              Bundle b = in.getExtras();
            final Integer click =  b.getInt("click");
             final Integer listclick =  b.getInt("listclick");
  
  
              //initilize all views...
              tv1 = (TextView) findViewById(R.id.textView_question);
              tv2 = (TextView) findViewById(R.id.textViewop1);
              tv3 = (TextView) findViewById(R.id.textViewop2);
              tv4 = (TextView) findViewById(R.id.textViewop3);
              tv5 = (TextView) findViewById(R.id.textViewop4);
              spinner = (Spinner) findViewById(R.id.spinner);
  
  
              //Initialize all array list....here data will come from mysql database or jsonarray...
              question = new ArrayList&lt;String&gt;();
              option1 = new ArrayList&lt;String&gt;();
              option2 = new ArrayList&lt;String&gt;();
              option3 = new ArrayList&lt;String&gt;();
              option4 = new ArrayList&lt;String&gt;();
              correct = new ArrayList&lt;Integer&gt;();
              q_id = new ArrayList&lt;Integer&gt;();
              id = new ArrayList&lt;Integer&gt;();
  
  
             //Set the different fonts....
              final Typeface myfont1 = Typeface.createFromAsset(getAssets(),"fonts/Vollkorn-
  
      Regular.ttf");
              final Typeface myfont2 = Typeface.createFromAsset(getAssets(),"fonts/PT_Serif-
  
      Web-Regular.ttf");
  
  
  
  
  
  
              //This is the request from server....for jsonarray.....
  
              //This is starting of jsonArrayrequest.......
              JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
  
      (Request.Method.POST, server_url,null,
                      new Response.Listener&lt;JSONArray&gt;() {
                          @Override
                          public void onResponse(JSONArray response) {
  
                              //response will come here....
  
                              int i = 0;
                              int j = response.length();
  
                              //retrieve all json object from json array...and fill that data to the 
  
      corresponding arraylist...
  
                              while (i&lt;j)
                              {
                                  try {
                                      JSONObject jsonObject = response.getJSONObject(i);
                                      id.add(jsonObject.getInt("id"));
                                      q_id.add(jsonObject.getInt("q_id"));
                                      question.add(jsonObject.getString("question"));
                                      option1.add(jsonObject.getString("op_1"));
                                      option2.add(jsonObject.getString("op_2"));
                                      option3.add(jsonObject.getString("op_3"));
                                      option4.add(jsonObject.getString("op_4"));
                                      correct.add(jsonObject.getInt("correct"));
  
  
                                      i++;
  
  
  
  
                                  } catch (JSONException e) {
                                      e.printStackTrace();
                                  }
  
                              }
  
                              //set fonts to all the views....
  
                              tv1.setTypeface(myfont1);
                              tv2.setTypeface(myfont1);
                              tv3.setTypeface(myfont1);
                              tv4.setTypeface(myfont1);
                              tv5.setTypeface(myfont1);
  
  
                              //set values to the views from arraylist....
  
                              tv1.setText(question.get(0));
                              tv2.setText(option1.get(0));
                              tv3.setText(option2.get(0));
                              tv4.setText(option3.get(0));
                              tv5.setText(option4.get(0));
  
  
  
  
                          }
                      }, new Response.ErrorListener() {
                  @Override
                  public void onErrorResponse(VolleyError error) {
  
  
                      //Error will appear here.....
                      tv1.setText("error happened...");
  
                  }
              }){
  
                  @Override
                  protected Map&lt;String, String&gt; getParams() throws AuthFailureError {
  
                      Map&lt;String , String&gt; params = new HashMap&lt;String, String&gt;();
  
                      //put values in the params....
                      params.put("category",""+click);
                      params.put("quizno",""+listclick);
  
                      //return params....and values to the php script...
                      return params;
                  }
              };
  
              //This is end of jsonArrayrequest.....
  
  
              //Create a new request queue.....
          RequestQueue requestQueue = Volley.newRequestQueue(this);
  
  
              //Add jsonarray request to the request queue....
         requestQueue.add(jsonArrayRequest);
  
  
  
  
  
              //End of oncreate method....
          }
  
  
  
  
          //End of activity class......
      }   
  
  
  and this is the php script..... 
  
  
  &lt;?php
  
  //get values from android app......
  
  $click = $_POST['category'];
  $listclick = $_POST['quizno'];
  
  
  //This is fix....
  $language = 1;
  
  
  
  
  
  
  
  $user = "root";
  $password = "";
  $host = "localhost";
  
  
  //All database names....
  
  $db_name1 = "history_english";
  $db_name2= "history_hindi";
  $db_name3= "world_english";
  $db_name4= "world_hindi";
  $db_name5= "science_english";
  $db_name6= "science_hindi";
  $db_name7= "sports_english";
  $db_name8= "sports_hindi";
  $db_name9= "india_english";
  $db_name10= "india_hindi";
  $db_name11= "computer_english";
  $db_name12= "computer_hindi";
  $db_name13= "politics_english";
  $db_name14= "politics_hindi";
  $db_name15= "business_english";
  $db_name16= "business_hindi";
  
  
  
  //Conditions for fetch jsonarray.....
  
  if($click==1)
  {
  
      //means history database....
  
      if($language==1)
      {
          //means english database...history_english
          $con1 = mysqli_connect($host,$user,$password,$db_name1);
  
  
  
  
          //Make sql query......
          $q1 = "select * from t_".$listclick;
  
          $result1 = mysqli_query($con1,$q1);
          $response = array( );
          $num1 = mysqli_num_rows($result1);
  
  
          //Encode data as jsonarray.....
  
          while($row1 = mysqli_fetch_array($result1))
          {
  
              array_push($response,array("id"=&gt;$row1["id"],"q_id"=&gt;$row1["q_id"],"question"=&gt;$row1["question"],"op_1"=&gt;$row1["op_1"],
              "op_2"=&gt;$row1["op_2"],"op_3"=&gt;$row1["op_3"],"op_4"=&gt;$row1["op_4"],"correct"=&gt;$row1["correct"]));
          }
  
  
          echo json_encode($response);
  
  
  
  
  
      }
      else
      {
          //means hindi database.....history_hindi
  
          $con2 = mysqli_connect($host,$user,$password,$db_name2);
  
  
          //Write code later.....
  
      }
  
  
  
  
  
  
  
  
  
  
  ?&gt;
  </code></pre>