2014-12-06 21 views
-1

我創建在其中,我使用SQLite的Android應用。我有一張桌子,裏面有5個領域。該表中有很多行,我想將所有使用JSON的行發送給我的PHP代碼,以便我可以接收該對象並將其插入到MySQL數據庫中。如何發佈多行(或列表對象)到PHP使用JSON(安卓)

什麼是做到這一點的最好辦法。我正在使用LIST對象,但不知道如何使用JSON使用POST。

請指導!

我使用這個代碼

mCursor =db.rawQuery("SELECT * FROM customer", null); 

      while (mCursor.moveToNext()) { 
       // In one loop, cursor read one undergraduate all details 
       // Assume, we also need to see all the details of each and every undergraduate 
       // What we have to do is in each loop, read all the values, pass them to the POJO class 
       //and create a ArrayList of undergraduates 
       customer = new Customer(); 
       customer.CustomerName = mCursor.getString(0).toString(); 
       customer.Phone = mCursor.getString(1).toString(); 
       customer.BrandName = mCursor.getString(2).toString(); 
       customer.City = mCursor.getString(3).toString(); 
       customer.FamilyMembers = mCursor.getString(4).toString(); 

       customerList.add(customer);     

      } 
for (int i = 0; i < customerList.size(); i++) 
       { 
        JSONObject row = new JSONObject(); 
        row.put("Customer", customerList.get(i));       
        json.put(row); 
       } 

不知道如何將它發佈到PHP代碼。

+0

打破你的問題了,從表中發送一個領域到PHP後端(這會給你的答案JSON和POST)。然後發送所有5個字段(這擴展了您的JSON使用)。然後發送多行(學習LIST界面)。那麼你會發現它更容易。 – Blundell 2014-12-06 20:30:40

+0

謝謝布朗德爾!我可以通過JSON輕鬆地將值對作爲參數。使用httpRequest。但我需要知道如何創建JSON對象列表,然後將其作爲JSON列表或字符串或任何使用HTTPRequest的東西發送。 – Abubaker 2014-12-07 09:44:42

+0

然後,你應該已經發布了一些代碼與你的問題。要發送一份清單,您可以將它作爲價值對中的值發送。您可以閱讀JSON規範以瞭解如何製作Json數組。 – Blundell 2014-12-07 10:25:15

回答

1

你必須通過你的列表進行迭代,並用一個JSONObjects每一行填寫一個JSONArray。在JSONObject中,可以使用鍵作爲列名,並使用所需類型的值。

JSONArray json = new JSONArray(); 
for (int i = 0; i < list.size(); i++) { 
    JSONObject row = new JSONObject(); 
    row.put("key1", list.get(i).value1); 
    ... 
    json.put(row); 
} 

(未測試)。然後,你可以發佈json.toString()到服務器,並與

$jsonstring = file_get_contents('php://input', true); 
檢索在PHP
+0

謝謝。我會試試這個。我將如何將該json傳遞給PHP? 是否這樣? HttpClient.SendHttpPost(Url,jsonObject); – Abubaker 2014-12-07 07:44:40

+0

我明白了。謝謝!有效! – Abubaker 2014-12-07 15:08:43