我正在尝试将HashMap发送到Android的Magento REST API。其余的包含一个数组元素,如下所示。
[{
"id": "26",
"label": "",
"position": "1",
"exclude": "0",
"url": "http:\/\/localhost\/magento\/media\/catalog\/product\/6\/1\/61UROlGlryL._UL1500_.jpg_20.jpg",
"types": []
}]
我尝试使用HttpPost
发送数据如下所示。
Map < String, String > productimages = new HashMap < String, String > ();
productimages.put("file_mime_type", mime);
productimages.put("file_content", encodedImage);
productimages.put("file_name", pictureName);
String[] datas = {
"image", "small_image", "thumbnail"
};
productimages.put("types", datas.toString());
Gson gson = new Gson();
String productimages_json = gson.toJson(productimages);
StringEntity productimages_entity = new StringEntity(productimages_json, HTTP.UTF_8);
HttpPost httppost_img = new HttpPost(URL_PRODUCTS + "/6/images");
httppost_img.setHeader("Content-Type", "application/json");
httppost_img.setHeader("Accept", "application/json");
httppost_img.setEntity(productimages_entity);
Log.d("inserted", "");
HttpResponse response_img = client.execute(targetHost, httppost_img, localContext);
所有数据而不是“类型”:[]正在点击Web服务。当像productimages.put("types", "image");
这样的单个字符串发送数据时,它成功点击了Web服务但我需要发送多个值。我也尝试了以下但没有结果。
Map<String,String[]> productimages = new HashMap<String, String[]>();
String[] datas = {"image","small_image","thumbnail"};
productimages.put("types", datas);
如何使用单个键将String []值发送到REST Web Service。任何人都可以帮我解决这个问题。
答案 0 :(得分:0)
尝试
for (int i = 0; i < datas.length; i++) {
productimages.put("types[" + Integer.toString(i) + "]", datas[i]);
}
答案 1 :(得分:-1)
正如先生:Jon Skeet建议我,我能够将数据作为对象发送如下。
Map<String,Object> productimages = new HashMap<String, Object>();
List<String> datas = new ArrayList<String>();
datas.add("image");
datas.add("small_image");
datas.add("thumbnail");
productimages.put("file_mime_type", mime);
productimages.put("file_content", encodedImage);
productimages.put("file_name", pictureName);
productimages.put("types", datas);