0
问题:如何在多部分http POST上发送嵌入式关联(name => value)子数组,而不仅仅是普通的单维顶级名称=>值对?我搜查了很久,无法找到一个这样的例子。在Android MultipartMode中发送多个嵌套的NameValue数组POST
问题:如何在多部分http POST上发送嵌入式关联(name => value)子数组,而不仅仅是普通的单维顶级名称=>值对?我搜查了很久,无法找到一个这样的例子。在Android MultipartMode中发送多个嵌套的NameValue数组POST
这是我对这个问题的解决方案:如何使用MultipartEntityBuilder实体从Android设备发送multipart文章中的name => value对的子数组。这对我来说很有用。它还发送一个图像文件,但其中的例子比比皆是,这里唯一的是嵌套的嵌套关联数组在相同的发布请求中。我希望这可以帮助别人。如果您发现问题,请随时指出。请原谅任何格式错误,因为这是我第一次在这里发帖。谢谢,斯科特
public String makePostRequest() {
public final static String INTENT_URL_PARAM = "server_url";
public final static String INTENT_API_PARAM = "server_api";
// Note: extras array is the incoming intent bundle, btw.
String url = extras.getString(INTENT_URL_PARAM);
String api = extras.getString(INTENT_API_PARAM);
url += "api/" + api; // add the api call to the base URL
HttpPost httpPost = new HttpPost(url);
String filePath = extras.getString("file_path");
File file = new File(filePath);
if (!file.exists()) {
Log.i(TAG, getClass().getSimpleName() + "file failed exists(); filePath = " + filePath);
}
else {
FileBody fileBody = new FileBody(file);
try {
Log.i(TAG, getClass().getSimpleName() + ": try {putting together multipart post request}");
HttpClient client = new DefaultHttpClient();
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
// add image file
builder.addPart("file", fileBody);
// add additional post data
builder.addTextBody("user_id", extras.getString("user_id"));
// assemble the item[] and item_media[] array.
builder.addTextBody("item[name]", extras.getString("name"));
builder.addTextBody("item[description]", extras.getString("description"));
builder.addTextBody("item[item_type]", extras.getString("item_type"));
builder.addTextBody("item[units]", extras.getString("units"));
builder.addTextBody("item_media[url]", extras.getString("item_url"));
builder.addTextBody("item_media[mime_type]", extras.getString("mime_type"));
builder.addTextBody("item_media[encoding]", extras.getString("encoding"));
if (extras.containsKey("hasGeoTag") && extras.getString("hasGeoTag").contentEquals("true")) {
// add geotag name => value data
builder.addTextBody("geolocation[lon]", extras.getString("lon"));
builder.addTextBody("geolocation[lat]", extras.getString("lat"));
builder.addTextBody("geolocation[ele]", extras.getString("ele"));
}
httpPost.setEntity(builder.build());
HttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseTmp = response.toString();
entity.consumeContent();
client.getConnectionManager().shutdown();
return responseTmp; // dumb string response for code sample. Use something more fitting yourself.
} catch (ClientProtocolException e) {
Log.i (TAG, "Http Post Failed. ClientProtocolException Exception: ");
// Log exception
e.printStackTrace();
} catch (IOException e) {
Log.i (TAG, "Http Post Failed. IOException Exception: ");
// Log exception
e.printStackTrace();
}
}
return "Some error string as you see fit.";
}
WWW服务器接收以下列格式该请求的数据(如印刷用PHP的print_r($ _ POST,真))
Array
(
[user_id] => 98
[item] => Array
(
[name] => Profile Image
[description] => image for user profile
[item_type] => 1
[units] => images
)
[item_media] => Array
(
[url] => null
[mime_type] => image/png
[encoding] => null
)
[geolocation] => Array
(
[lon] => -133.03752347
[lat] => 23.8897191
[ele] => 229.89999389648438
)
)
欢迎SO。你不应该发表一个答案作为问题。如果您将原始问题作为一个问题陈述,然后将解决方案作为对该问题的答案发布,那将会更有意义。 – honk 2014-09-03 16:49:02
感谢协议更正。我会尝试绕回并重新格式化我的帖子,以符合问题然后回复发布格式。 -SM – FlannelTuba 2014-09-03 17:09:13