我必须将JSONArray的对象值传递给php,然后将对象的所有数据插入mysql数据库。下面是我在安卓中的代码,
主要活动.java
private void uploadUserId_FriendId(String user_id , JSONArray friend_id) {
try {
try {
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, System.getProperty("http.agent"));
HttpPost httpPost = new HttpPost("http://192.168.0.106/demo/demo.php");
MultipartEntity multipartEntity = new MultipartEntity();
multipartEntity.addPart("user_id", new StringBody(user_id));
// Here I have to add JsonArray object "friend_id", but it is not allowing me to add in //multipartentity
System.out.print("Test" + friend_id);
// JSONArray(friend_id);
httpPost.setEntity(multipartEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
if(httpResponse != null) {
} else { // Error, no response.
Toast.makeText(getBaseContext(), "Server Error. ", 2000).show();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
但是在这里,我不知道如何使用MultipartEntity对象添加JSONArray对象,每次我尝试使用MultipartEntity的对象添加时,它都不会显示任何输出。如何使用多部分实体的对象添加 JSONArray 数据?
就我而言,我将 JSONArray 放入 JSONObject 中并发送它。我的setEntity
和SetHeader
代码如下所示
record.put("array", myJSONArray);
httpPost.setEntity (new ByteArrayEntity (record.toString().getBytes ("UTF8")));
httpPost.setHeader ("data", record.toString());
这里的记录是我的JSONObject,其中包含我的JSONArray。