2013-02-08 31 views
0

使用此代碼,我將信息從本地sqlite數據庫發送到我的服務器(mysql),並且工作正常。但我只發送table1的數據。如果我想將我所有的sqlite表的數據發送到我的服務器,我該怎麼做?如何將數據從多個表(db.sqlite)發送到php

public class sendInformation { 
public SQLiteDatabase newDB; 
JSONObject jObject; 
JSONArray jArray = new JSONArray(); 

public void getValues() 
{ 
    try 
    { 
     newDB = SQLiteDatabase.openDatabase("/data/data/com..../databases/...db",null,SQLiteDatabase.CONFLICT_NONE);  
    }catch(SQLException e) 
    { 
     Log.d("Error","Error while Opening Database"); 
     e.printStackTrace(); 
    } 
    try 
    { 
     Cursor c = newDB.rawQuery("Select * from table1",null); 
     if (c.getCount()==0) 
     { 
      c.close(); 
      Log.d("","no items on the table");    
     }else 
     { 
      c.moveToFirst();  

      while(c.moveToNext()) { 
       jObject = new JSONObject(); 
       jObject.put("ID", c.getString(c.getColumnIndex("ID"))); 
       jObject.put("Notes", c.getString(c.getColumnIndex("Notes"))); 
       jObject.put("Cellphone", c.getString(c.getColumnIndex("Cellphone"))); 
       jObject.put("Date", "null"); 
       jObject.put("Address", "null"); 
       jArray.put(jObject); 
      } 

      c.close(); 
      Log.d("","ALL the data in the DB"+jArray.toString()); 
      int arrayLength=jArray.length(); 
      Log.d("","Lenght of the jArray"+arrayLength); 
      HttpParams httpParams = new BasicHttpParams(); 
      HttpConnectionParams.setConnectionTimeout(httpParams,9000); 
      HttpConnectionParams.setSoTimeout(httpParams, 9000); 

      HttpClient client = new DefaultHttpClient(httpParams); 

      String url = "http://ipaddress/..../test.php?arrayLength="+arrayLength; 

      HttpPost request = new HttpPost(url); 
      request.setEntity(new ByteArrayEntity(jArray.toString().getBytes("UTF8")));  
      request.setHeader("json", jArray.toString()); 

      HttpResponse response = client.execute(request); 
      HttpEntity entity = response.getEntity(); 
      // If the response does not enclose an entity, there is no need 

      if (entity != null) { 
       InputStream instream = entity.getContent(); 

       String result = RestClient.convertStreamToString(instream); 
       Log.i("Read from server", result); 
      } 
     }  
    } catch (UnsupportedEncodingException uee) { 
     Log.d("Exceptions", "UnsupportedEncodingException"); 
     uee.printStackTrace(); 
    }catch (Throwable t) { 
     Log.d("","request fail"+t.toString()); 
    } 

    this.newDB.close(); 
    } 
} 

正如你所看到的,我從table1的所有數據,並創建一個JSONObject。

  • 我把它傳遞給test.php文件,但如果我從table2,table3 ...做另一個查詢,我必須把第二個和第三個表的值放在同一個JsonObject中?
  • 如果我這樣做,我將如何知道哪些數據來自哪個表?
  • 如果我可以發送倍數jsonobject,我該怎麼做,我將如何接收我的test.php文件中的數據。

現在我做它$json = file_get_contents('php://input');

回答

0

你已經有一半的解決方案。只需複製爲每個附加表創建並填充JsonObject的代碼,然後將這些添加到JsonArray中即可。您可以使用另一個JsonObject - 而不是JsonArray - 它可以包含其他JsonObjects。

在服務器上,只需使用json_decode($ json,true),您將以陣列數組的形式獲取數據 - 將結果轉儲到帶有print_r()的文件,以便您可以看到數據的結構。

+0

如何將JSONobject添加到另一個JSONobject? – user2033349

+0

我只對所有的表進行同樣的處理,並且在每個表格放置jArray.put(「//」)後發送jArray中的所有信息,所以我將數據從不同的表格中分離出來 – user2033349

相關問題