我必须将 JSONArray 的对象值传递给 php,然后将对象的所有数据插入 mysql 数据库。下面是我在android中的代码,
MainActivity.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 的对象添加时,它都不会显示任何输出。如何使用 MultipartEntity 的对象添加 JSONArray 数据?