【发布时间】:2014-08-14 07:19:37
【问题描述】:
我正在尝试从 Android 向 Magento REST API 发送 HashMap。其余的包含一个数组元素,如下所示。
[{
"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"); 之类的单个字符串发送时,它成功地访问了网络服务但我需要发送多个值。我也尝试了以下但没有结果。
Map<String,String[]> productimages = new HashMap<String, String[]>();
String[] datas = {"image","small_image","thumbnail"};
productimages.put("types", datas);
如何使用单个键将 String[] 值发送到 REST Web 服务。请帮助我解决这个问题。
【问题讨论】:
-
“但没有结果”并没有告诉我们您所观察到的任何信息。什么是通过网络发送的,你是如何观察到的?请注意,到目前为止您显示的代码甚至无法编译,特别是这一行:
productimages.put("types", datas.toString);因为toString()是一个方法,而不是一个字段。您向我们展示的代码与您的真实代码之间还有多少其他差异? -
我没有把我的完整代码放在这里,我只是把我试过的东西放在这里。 'productimages.put("types", datas.toString);'线剂量没有显示任何错误。如果您知道,请告诉我如何将 String[] 值发送到 Web 服务。
-
恐怕我不相信你。那行代码根本不会编译。你需要
productimages.put("types", data.toString())。注意额外的括号。你还没有向我们解释你所观察到的…… -
@Jon Skeet,@ 抱歉,现在我编辑了我的问题。
-
对 - 你看过
datas.toString()会返回什么吗?您是否尝试过使用Map<String, Object>代替?
标签: java android api rest magento