Sending ArrayList from Android to PHP script using JSON

前端 未结 3 1311
轮回少年
轮回少年 2020-12-10 00:06

What is the Scenario

I want to send multiple ArrayList (usally 5) from android to the server and want to insert it into mysql database.

3条回答
  •  爱一瞬间的悲伤
    2020-12-10 00:54

    You cant send Arraylist to server,its an object. The best way to solve your problem is user JSON , you need to do something like that -

     ArrayList Questions=  new ArrayList();
     ArrayList A1=  new ArrayList();
     ArrayList A2=  new ArrayList();
     ArrayList A3=  new ArrayList();
     ArrayList A4=  new ArrayList();
    
    
     JsonArray jArr1= new JsonArray();
     for(String data:A1)
     {
       jArr1.add(data);
     }
      JsonArray jArr2= new JsonArray();
     for(String data:A2)
     {
       jArr2.add(data);
     }
     //convert each array list to jsonarray
      JsonArray jArraySet = new JsonArray();
      jArraySet.add(jArr1);
      jArraySet.add(jArr2);
    
     //add each json array to jArraySet
     // then send the data via 
       HttpClient httpclient = new DefaultHttpClient();
       ArrayList nameValuePairs = new ArrayList();                   
        // put the values of id and name in that variable
       nameValuePairs.add(new BasicNameValuePair("all_arraylist",jArraySet.toString()));
       HttpPost httppost = new HttpPost(url);
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity();
    

    note: dont forget to do it in asynctask

    in php section ,do the following

    
    

提交回复
热议问题