2015-04-19 22 views
0

我想將JSONArrayAndroid發佈到webserver;在JSONArray包含recordsSQLite數據庫表從Android如何知道新記錄被檢索到JSONArray中?

JSONObject parameters = new JSONObject(); 
      parameters.put("jsonArray", new JSONArray(Arrays.asList(makeJSON()))); 
      parameters.put("type", "Android"); 
      URL url = new URL(myurl); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setRequestProperty("Content-Type", "application/json"); 
      conn.setDoOutput(true); 
      conn.setRequestMethod("POST"); 
      OutputStream out = new BufferedOutputStream(conn.getOutputStream()); 
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); 
      writer.write(parameters.toString()); 
      writer.close(); 
      out.close(); 

方法makeJSON:

if(mCur != null && mCur.moveToFirst()){ // method makeJSON() retrieving database records 
      do{ 
       try{ 
        JSONObject jObj = new JSONObject(); 
        String notificationDateFor = mCur.getString(mCur.getColumnIndexOrThrow("NotificationDateFor")); 
        String typeNotification = mCur.getString(mCur.getColumnIndexOrThrow("TypeNotification")); 
        String dob = mCur.getString(mCur.getColumnIndexOrThrow("DOB")); 
        String friendsName = mCur.getString(mCur.getColumnIndexOrThrow("FriendsName")); 
        String imageUri = mCur.getString(mCur.getColumnIndexOrThrow("imageUri")); 

        jObj.put("NotificationDateFor", notificationDateFor); 
        jObj.put("DOB", dob); 
        jObj.put("TypeNotification", typeNotification); 
        jObj.put("FriendsName", friendsName); 
        jObj.put("imageUri", imageUri); 
        jArr.put(jObj); 

       }catch(Exception e){ 
        System.out.println("Exception: "+e); 
       } 
      }while(mCur.moveToNext()); 

     } 
     return jArr; 

PHP腳本:

$jsonArray = stripslashes($_POST['jsonArray']); 

foreach ($jsonArray as $obj) { 
    ... // how to know if a new record is being treated ? 
} 

裏面的PHP腳本如何知道我是否創造新紀錄?因爲我想從JSONArray插入/更新每條記錄。

回答

0

查看數據是否被髮表呼應出來,像這樣:

$jsonArray = stripslashes($_POST['jsonArray']); 

foreach ($jsonArray as $k=>$v) { 
    echo $k . ' - ' . $v . '<br>'; 
}