在我以前使用NameValuePair
的帖子请求中Map<String, Object> data = new LinkedHashMap<String, Object>();
data.put(SESSION, sessionId);
data.put(MODULE_NAME, "POD_POD");
try {
JSONArray nameValueArray = new JSONArray();
// signature_path_value.put("name", "uploadfile");
//signature_path_value.put("value", image_str);
//nameValueArray.put(signature_path_value);
customer_value.put("name", "signed_by");
customer_value.put("value", customer);
nameValueArray.put(customer_value);
order_time_value.put("name", "delivered_on");
order_time_value.put("value", order_time);
nameValueArray.put(order_time_value);
user_value.put("name", "driver_name");
user_value.put("value", user);
nameValueArray.put(user_value);
data.put(NAME_VALUE_LIST, nameValueArray);
String restData = org.json.simple.JSONValue.toJSONString(data);
HttpClient httpClient = new DefaultHttpClient();
HttpPost req = new HttpPost(rest_url);
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(METHOD, SET_ENTRY));
nameValuePairs.add(new BasicNameValuePair(INPUT_TYPE, JSON));
nameValuePairs.add(new BasicNameValuePair(RESPONSE_TYPE, JSON));
nameValuePairs.add(new BasicNameValuePair(REST_DATA, restData));
req.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Send POST request
httpClient.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
HttpResponse res = httpClient.execute(req);
但现在我使用MultiPartEntity将Image上传到服务器..我知道使用'myEntity.addPart()'可以将字符串值添加到multipartentity。但是我如何将这些行添加到multiPartEntity
JSONArray nameValueArray = new JSONArray();
// signature_path_value.put("name", "uploadfile");
//signature_path_value.put("value", image_str);
//nameValueArray.put(signature_path_value);
customer_value.put("name", "signed_by");
customer_value.put("value", customer);
nameValueArray.put(customer_value);
order_time_value.put("name", "delivered_on");
order_time_value.put("value", order_time);
nameValueArray.put(order_time_value);
user_value.put("name", "driver_name");
user_value.put("value", user);
nameValueArray.put(user_value);
data.put(NAME_VALUE_LIST, nameValueArray);
String restData = org.json.simple.JSONValue.toJSONString(data);
答案 0 :(得分:2)
我给出了如何将MultipartEntity
与字符串和文件数据一起使用的示例代码。
MultipartEntity mEntity = new MultipartEntity();
//For String data
mEntity.addPart("key1", new StringBody("value1"));
mEntity.addPart("key2", new StringBody("value2"));
mEntity.addPart("key3", new StringBody("value3"));
//For File data
mEntity.addPart("file", new FileBody("file_path"));
req.setEntity(mEntity);
在你的情况下尝试下面的代码。
mEntity.addPart(METHOD, new StringBody(SET_ENTRY));
mEntity.addPart(INPUT_TYPE, new StringBody(JSON));
mEntity.addPart(RESPONSE_TYPE, new StringBody(JSON));
mEntity.addPart(REST_DATA, new StringBody(restData));