2016-10-01 1 views
0

JSON 양식이 있습니다.JSON을 Android의 Volley 필드에 http 양식으로 작성

{ 
    "music":[ 
     { 
     "acrid":"65468a1a4d0cc5c14b058b81783803b8", 
     "artists":[ 
      { 
       "name":"bond" 
      } 
     ], 
     "title":"Explosive", 
     "genres":[ 
      { 
       "name":"Classical" 
      }, 
      { 
       "name":"Jazz" 
      } 
     ], 
     "external_ids":{ 
      "isrc":"GBBBA0343510", 
      "upc":"028947678472" 
     }, 
     "album":{ 
      "name":"The Classical Album 2005" 
     }, 
     "duration_ms":"190360", 
     "release_date":"2005-01-01", 
     "label":"Universal Music Ireland Ltd.", 
     "result_from":3, 
     "external_metadata":{ 
      "youtube":{ 
       "vid":"HiaOFOMPOBc" 
      }, 
      "spotify":{ 
       "track":{ 
        "id":"0anmeZi9dLi00ZI4iBikOK" 
       }, 
       "artists":[ 
        { 
        "id":"3G4zK7ipHdaAZkG6EBwIoW" 
        } 
       ], 
       "album":{ 
        "id":"3IOUjGF4HmHXVMCur0VYUm" 
       } 
      }, 
      "itunes":{ 
       "track":{ 
        "id":1104915761 
       }, 
       "artists":[ 
        { 
        "id":723736030 
        } 
       ], 
       "album":{ 
        "id":1104914378 
       } 
      }, 
      "musicstory":{ 
       "track":{ 
        "id":"1540446" 
       } 
      }, 
      "deezer":{ 
       "track":{ 
        "id":2513417 
       }, 
       "genres":[ 
        { 
        "id":98 
        } 
       ], 
       "artists":[ 
        { 
        "id":8109 
        } 
       ], 
       "album":{ 
        "id":246770 
       } 
      } 
     }, 
     "play_offset_ms":98273 
     } 
    ], 
    "timestamp_utc":"2015-07-01 14:44:37" 
} 

나는 서버에 API 요청에 매개 변수로 사용하는 형태

data[music][acrid]=65468a1a4d0cc5c14b058b81783803b8" 
data[music][album][name]=bond 
.... 
data[music][genres][][name]=Classical 
data[music][genres][][name]=Jazz 
... 

의지도로 변환 사용해야합니다. Android에서 Volley를 사용하여 네트워크 요청을 보내고 있으므로 요청 매개 변수를 만들기 위해 Map이 필요합니다.

어떻게 그렇게합니까?

답변

1

내가 2 개 컨버터 기능 썼다 :

//add prefix to every map elemement 
    public static Map<String,String> addPrefixToMap(Map<String,String> map,String prefix) { 
     Map<String,String> result=new HashMap<>(); 
     for (String key:map.keySet()) { 
      result.put(prefix+key,map.get(key)); 
     } 
     return result; 
    } 

    /* 
     ConvertS JSON to form which can be used as params for Volley HTTP posting 
     Supported types:JSONObject, JSONArray,Integer,Long,Double,String 
     .toString is called for everything else 
     Exception handling is caller's responsibility 
    */ 
    public static Map<String,String> encodeJSONToMapWithPrefix(JSONObject json,String prefix2) throws JSONException, UnsupportedEncodingException { 
     Map<String,String> result=new HashMap<>(); 
     Iterator<String> keys = json.keys(); 
     String keyPrefix=""; 
     if (prefix2!=null) { 
      keyPrefix=prefix2; 
     } 
     while (keys.hasNext()) { 
      String key = keys.next(); 
      Object value= json.get(key); 

      if (value instanceof JSONObject) { 
       Map<String, String> r = encodeJSONToMapWithPrefix((JSONObject) value, keyPrefix + "[" + key + "]"); 
       result.putAll(r); 

      } else if (value instanceof JSONArray) { 
       JSONArray jarr=(JSONArray)value; 
       for (int i=0;i<jarr.length();i++) { 
        Map<String, String> r = encodeJSONToMapWithPrefix(jarr.getJSONObject(i), keyPrefix + "[" + key + "][]"); 
        result.putAll(r); 
       } 
      } else if (value instanceof Integer) { 
       result.put(keyPrefix+"["+key+"]",Integer.toString((Integer)value)); 
      } else if (value instanceof Long) { 
       result.put(keyPrefix+"["+key+"]",Long.toString((Long)value)); 
      } else if (value instanceof Double) { 
       result.put(keyPrefix+"["+key+"]",Double.toString((Double)value)); 
      } else if (value instanceof String) { 
       result.put(keyPrefix+"["+key+"]",(String)value); 
      } else { 
       result.put(keyPrefix+"["+key+"]",value.toString()); 
      } 
     } 
     return result; 
    } 

그것은 그 이후 쉬웠다을, 발리의의 getParams 기능은

 @Override 
     protected Map<String, String> getParams() { 
      Map<String, String> params = new HashMap<String, String>(); 

      try { 
       JSONObject json=new JSONObject(); 
       json.put("music",musicItem); 
       Map<String, String> mm=encodeJSONToMapWithPrefix(json,null); 
       mm=Utils.addPrefixToMap(mm,"data"); 

       params.putAll(mm); 
      } catch (JSONException e) { 
       CustomLog.logException(e); 
      } catch (UnsupportedEncodingException e) { 
       CustomLog.logException(e); 
      } 
      return params; 
     } 
입니다
관련 문제