我想将JSONArray
从Android
发布到webserver
;来自JSONArray
的{{1}}数据库表中的records
包含SQLite
:
Android
方法makeJSON:
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();
PHP脚本:
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中的每条记录。
答案 0 :(得分:0)
通过回显来查看是否正在发布数据,如下所示:
$jsonArray = stripslashes($_POST['jsonArray']);
foreach ($jsonArray as $k=>$v) {
echo $k . ' - ' . $v . '<br>';
}