2017-04-17 36 views
1

目前我可以單獨插入字符串值到我的MySQL數據庫使用凌空,像這樣:如何在Volley中將我的Arraylist插入到mysql中?

我創建兩個字符串:

public static final String KEY_PHONENUMBER = "phonenumber"; 
String phoneNo; 
在我的排球類

再往上:

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

然後在我的PHP端有這樣的:

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

所以,如果我指定phonenumber爲例如1234567890這將被插入到我的分貝。

上面的Volley代碼應該如何顯示,如果我想一次性發布整個電話號碼列表到我的數據庫?

我有一個ArrayList,alContacts,這看起來是這樣的:

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

我想在我的數據庫中插入所有的數字。 Volley代碼如何去做這件事?

我想我的PHP代碼將沿着線的東西:

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

但我會擔心抽射代碼之後。

回答

2

Firts您在PHP端發送的ArrayList與鍵,值對,

後,

$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); 
     } 

同時檢查this你可以瞭解如何創建的JSONObject和發送的ArrayList斷絕。

3

要將單個參數中的所有值發送到服務器,請使用JSONObject.Create使用所有鍵值的json對象。

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 
    } 

,然後使用PHP

$CheckContact = $_POST['alContacts']; 

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

編輯: 我認爲ü有一個數組和u可以存儲在JSON對象數組值。

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

我不明白for(int i = 1 etc ... arr [i]);}適用於我的情況。它是相關的嗎? – CHarris

+0

更新答案,因爲你問 – Vivek

相關問題