【问题标题】:How to send String[] As value of HashMap to REST如何将 String[] 作为 HashMap 的值发送到 REST
【发布时间】: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&lt;String, Object&gt; 代替?

标签: java android api rest magento


【解决方案1】:

试试

for (int i = 0; i < datas.length; i++) {
    productimages.put("types[" + Integer.toString(i) + "]", datas[i]);
}

【讨论】:

  • 很抱歉,这对我不起作用。
  • @RemeesMSyde 你能描述一下这段代码遇到的问题吗?
  • 当我尝试获取响应时,传递的字符串 [] 值没有进入响应。所有其他传递的值都得到了正确的响应。
【解决方案2】:

按照 Mr: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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 2014-07-20
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多