Search code examples
javaandroidmagentomagento-rest-api

How to send String[] As value of HashMap to REST


I am trying to send a HashMap to Magento REST API From Android. The rest contain a array element as showing below.

[{
    "id": "26",
    "label": "",
    "position": "1",
    "exclude": "0",
    "url": "http:\/\/localhost\/magento\/media\/catalog\/product\/6\/1\/61UROlGlryL._UL1500_.jpg_20.jpg",
    "types": []
}] 

I tried to send the data using HttpPost As showing below.

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);

All the data rather than "types":[ ] is hitting the web service. When send the data as single string like productimages.put("types", "image"); it hit the web service Successfully But I need to send more than one value.Also I tried the following but no result.

Map<String,String[]> productimages = new HashMap<String, String[]>();
String[] datas = {"image","small_image","thumbnail"};
productimages.put("types", datas);

How I can send the String[] values with the single key to REST Web Service.Anyone please help me to solve this.


Solution

  • As Mr:Jon Skeet suggest me I am able to send the data as object as below.

    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);