1

At present I can insert string values individually into my Mysql db using Volley, like this :

I create two strings :

public static final String KEY_PHONENUMBER = "phonenumber"; String phoneNo; 

And then further on in my Volley class :

@Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<String, String>(); map.put(KEY_PHONENUMBER, phoneNo); return map; 

And then on my PHP side something like :

 $CheckContact = $_POST['phonenumber']; etc.... 

So if I specify phonenumber as for example 1234567890 this will be inserted into my db.

How should the Volley code above look like if I want to post a whole arraylist of phone numbers into my db in one go ?

I have an arraylist, alContacts, which looks something like this :

[+12345, +34567, +65221, etc....] 

I want to insert all the numbers in my db. How would the Volley code go for this ?

I think my PHP code will be something along the lines of :

foreach($_POST['phonenumber' as $CheckContact] 

But I'll worry about that after the Volley code.

2 Answers 2

3

Firts you send arraylist with key,value pair,

After in php side,

$json = $_POST['phonenumber']; /* this is arraylist name */ $json_array = json_decode($json,true) $count = count($json_array ); for($i=0;$i<$count;$i++) { $phonenumber= $arr_obj[$i]->phonenumber; /*phonenumber is key of phonenuber value inside arraylist */ if(!empty($phonenumber)) { $insert = " insert into table_name(phonenumber) values('$phonenumber')"; $result = mysql_query($insert); } 

Also check this you can understand how to create jsonobject and send arraylist to sever.

Sign up to request clarification or add additional context in comments.

Comments

1

To send all values in single parameter to server use JSONObject.Create a json object using all key-value.

 JSONObject jsonObject=new JSONObject(); for(int i=1;i<=7;i++) { arr[i]="questionId_"+i+"_"+"ans_"+i; jsonObject.put("params_"+i,arr[i]); } @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<String, String>(); map.put("KEY_PHONENUMBER", jsonObject.toString()); return map } 

and then using php

 $CheckContact = $_POST['alContacts']; foreach($_POST['phonenumber' as $CheckContact]{ //insert into db } 

Edit: I think u have an array and u can store array value in json object.

 for(int i=1;i<=alContacts.length;i++) { jsonObject.put("params_"+i,alContacts.length[i]); } 

1 Comment

I don't understand how the for(int i = 1 etc...arr[i]);} applies in my case. Is it relevant ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.