这是我的代码,为什么在发送Multipart请求后服务器仍然看到我的代码如字符串而不是hashmap?
String token2= mPreferences.getString("auth_token","");
// open a URL connection to the Servlet
fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
//create json object
try {
spot.put("title", "titolo1");
spot.put("latitude", "1");
spot.put("longitude", "1");
spot.put("address", "antani");
spot.put("country", "IT");
spot.put("shot_level", "pro");
spot.put("photo_attributes",photo_attributes);
photo_attributes.put("0",attributes);
attributes.put("description","Nesciunt dignissimos qui non. Beatae optio eveniet quis..");
attributes.put("our_policies","1");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Convert json to hashmap
HashMap<String, String> attributesmap = new HashMap<String, String>();
JSONObject attributesObject = new JSONObject(attributes.toString());
HashMap<String, String> photo_attributesmap = new HashMap<String, String>();
JSONObject photo_attributesObject = new JSONObject(photo_attributes.toString());
HashMap<String, String> map = new HashMap<String, String>();
JSONObject spotjObject = new JSONObject(spot.toString());
Iterator<?> keys = spotjObject.keys();
Iterator<?> keys2 = photo_attributesObject.keys();
Iterator<?> keys3 = attributesObject.keys();
while( keys3.hasNext() ){
String key3 = (String)keys3.next();
String value3 = attributesObject.getString(key3);
attributesmap.put(key3, value3);
}
while( keys2.hasNext() ){
String key2 = (String)keys2.next();
String value2 = photo_attributesObject.getString(key2);
photo_attributesmap.put(key2, value2);
}
while( keys.hasNext() ){
String key = (String)keys.next();
String value = spotjObject.getString(key);
map.put(key, value);
}
File file = new File(sglimagepath);
try {
client = new DefaultHttpClient();
String postURL = upLoadServerUri;
post = new HttpPost(postURL);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
ContentBody con =new StringBody(map.toString());
reqEntity.addPart("spot", con );
post.setHeader("x-auth-token", token2);
post.setEntity(reqEntity);
HttpResponse response2 = client.execute(post);
HttpEntity resEntity = response2.getEntity();
if (resEntity != null) {
Log.i("RESPONSE", EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
这是错误:
Invalid parameter 'spot' value
"{address=antani, latitude=1,
shot_level=pro, country=IT,
title=titolo1,
photo_attributes={\"0\":{\"description\":\
"Nesciunt dignissimos qui non. Beatae optio eveniet quis..\",
\"our_policies\":\"1\"}}, longitude=1}": Must be a Hash)
这就是服务器想要的:
Parameters: {"spot"=> {"address"=>"87 The Dr London W3 6AG, UK", "country"=>"United Kingdom", "description"=>"Descrizione prova panorama", "event_type"=>"undisclosed", "latitude"=>"51.51636627452077", "longitude"=>"-0.2641945821234576", "needs_flash"=>"false", "needs_tripod"=>"false", "shot_level"=>"easy", "things_to_avoid"=>"", "things_to_watch"=>"", "title"=>"View panorama", "photos_attributes"=>{"0"=>{"ref"=>#<ActionDispatch::Http::UploadedFile:0x007fb685cb1680 @original_filename="photo0.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"spot[photos_attributes][0][ref]\"; filename=\"photo0.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<Tempfile:/tmp/RackMultipart20151010-325-1qd3fl1>>}}}}
服务器想要的基本上是photo_attributes不像String,而是像HashMap。我试着去做但没什么...... 在此先感谢你们