title: Sending ArrayList from Android to PHP script using JSON
body: <p><strong>What is the Scenario</strong></p>
  
  <p>I want to send multiple ArrayList (usally 5) from android to the server and want to insert it into mysql database.  </p>
  
  <p><strong>What I Have Done Successfully</strong></p>
  
  <p>I have Successfully send single value and Multiple Values from Android to PHP script using JSON</p>
  
  <p>i have Received single and Multiple Records from mysql database to android using JSON</p>
  
  <p>Here is the Code for inserting and getting value from server </p>
  
  <pre><code> class TeacherLogin1 extends AsyncTask&lt;Void, Void, Void&gt; {
  
           String name,pass;
           Context contextt;
           int idofteach = 0;
           int codee = 0;
  
           TeacherLogin1(String pass1,String name1,Context context)
           {
               name = name1;
               pass = pass1;
               contextt  = context;
           }
  
           @Override
           protected void onPreExecute() {
           super.onPreExecute();
           }
  
  
           @Override
           protected Void doInBackground(Void... arg0) {
                codee = Authenticate(name,pass);
  
           return null;
           }
  
           @Override
           protected void onPostExecute(Void result) {
           super.onPostExecute(result);
           if(codee!=0)
           {
               Intent teachers = new Intent(context,TeachersView.class);
               teachers.putExtra("TID", codee);
               teachers.putExtra("TNAME", TeachName);
               teachers.putExtra("sub1", subj1);
               teachers.putExtra("sub2", subj2);
               teachers.putExtra("sub3", subj3);
               teachers.putExtra("sub4", subj4);
              startActivity(teachers);
           }
           else
               Toast.makeText(context, "Wrong Details", Toast.LENGTH_SHORT).show();
           codee = 0;
           }
           } 
       public int Authenticate(String name,String Pass)
       {
  
                  ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
  
                // put the values of id and name in that variable
              nameValuePairs.add(new BasicNameValuePair("name",name));
              nameValuePairs.add(new BasicNameValuePair("pass",Pass));
  
  
  
                  try
                  {
                  HttpClient httpclient = new DefaultHttpClient();
                   ScriptsFilePath a = new ScriptsFilePath();
                  HttpPost httppost = new HttpPost(a.TeacherAuthen);
  
                      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                      HttpResponse response = httpclient.execute(httppost); 
                      HttpEntity entity = response.getEntity();
                      is = entity.getContent();
                      Log.e("pass 1", "connection success ");
              }
                  catch(Exception e)
              {
                      Log.e("Fail 1", e.toString());
              }     
  
                  try
                  {
                      BufferedReader reader = new BufferedReader
                      (new InputStreamReader(is,"iso-8859-1"),8);
                      StringBuilder sb = new StringBuilder();
                      while ((line = reader.readLine()) != null)
                  {
                          sb.append(line + "\n");
                      }
                      is.close();
                      result = sb.toString();
                  Log.e("pass 2", "connection success ");
              }
                  catch(Exception e)
              {
                      Log.e("Fail 2", e.toString());
              }     
  
  
  
                        try {
  
                         JSONObject jsonResponse = new JSONObject(result);
                         if(jsonResponse != null)
                         {
                         JSONArray jsonMainNode = jsonResponse.optJSONArray("GetTeacher");
  
                         for (int i = 0; i &lt; jsonMainNode.length(); i++) {
                          JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                          code = jsonChildNode.optInt("ID");
                          TeachName= jsonChildNode.optString("Name");
                          subj1= jsonChildNode.optString("subject1");
                          subj2= jsonChildNode.optString("subject2");
                          subj3= jsonChildNode.optString("subject3");
                          subj4= jsonChildNode.optString("subject4");
                         }
  
                         }
  
                        } catch (JSONException e) {
                  //     Toast.makeText(getApplicationContext(), "Error" + e.toString(),
                  //       Toast.LENGTH_SHORT).show();
                        }
                      return code;
  
  
       }
  </code></pre>
  
  <p>and the TeacherAuthen.php script</p>
  
  <pre><code>&lt;?php
  error_reporting(E_ALL ^ E_DEPRECATED);
      $host="localhost";
      $uname="root";
      $pwd='';
      $db="examsystem";
  
      $con = mysql_connect($host,$uname,$pwd) or die("connection failed");
      mysql_select_db($db,$con) or die("db selection failed");
  
       if(isset($_REQUEST)){
      $name=$_REQUEST['name'];
      $pass=$_REQUEST['pass'];}
  
      $flag['code']=0;
      $name1['code1'] = "sdf";
  
  $sql = "SELECT * FROM teachers WHERE Username ='$name' and Pass='$pass'";
  $result = mysql_query($sql);
  $json = array();
  
  if(mysql_num_rows($result)){
  while($row=mysql_fetch_assoc($result)){
  $json['GetTeacher'][]=$row;
  }
  }
  echo json_encode($json);
  mysql_close($con);
  ?&gt; 
  </code></pre>
  
  <p><strong>where i am stuck</strong></p>
  
  <p>I dont no how to send ArrayList from android to PHP Script.</p>
  
  <p>For Example I want to send these arraylists to php script =</p>
  
  <pre><code>ArrayList&lt;String&gt; Questions=  new ArrayList&lt;String&gt;();
   ArrayList&lt;String&gt; A1=  new ArrayList&lt;String&gt;();
   ArrayList&lt;String&gt; A2=  new ArrayList&lt;String&gt;();
   ArrayList&lt;String&gt; A3=  new ArrayList&lt;String&gt;();
   ArrayList&lt;String&gt; A4=  new ArrayList&lt;String&gt;();
  </code></pre>
  
  <p>and then in the PHP script i want to insert like this</p>
  
  <pre><code>"INSERT INTO `Question` (`ID` ,`Question` ,`A1` ,`A2` ,`A3` , `A4` ) VALUES (NULL, '$Question', '$A1', '$A2', '$A3' ,'$A4'); "
  </code></pre>
  
  <p>if i am able to send even a single arrayList then i think i can make a way to do the above</p>
  
  <p>and Thanks for you Time</p>