我必须将JSONArray的对象值传递给php,然后将对象的所有数据插入到mysql数据库中。下面是我在android中的代码,
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的对象添加时,它都不会显示任何输出。如何使用MultipartEntity的对象添加JSONArray数据?
答案 0 :(得分:0)
在我的情况下,我将JSONArray放在JSONObject中并发送它。
我的setEntity
和SetHeader
代码如下所示
record.put("array", myJSONArray);
httpPost.setEntity (new ByteArrayEntity (record.toString().getBytes ("UTF8")));
httpPost.setHeader ("data", record.toString());
这里的记录是我的JSONObject,它包含我的JSONArray。