2017-08-28 11 views
2

私はそれに多くのオブジェクトを持つjsonのAPIを持っている、私はキーの後にマップにデータを格納したい。それから私は後でそのマップリストを使いたい。私がそれをしたいと思う方法は以下の通りです。Volleyを使用してhttp getリクエストを送信し、キーを使用してMapにデータを保存するにはどうすればよいですか?

Map<Integer, Integer> maplist; /// globle variable 
 
//// inside response method inside the for loop 
 

 
    maplist = new HasMap<>(); 
 

 
maplist.put(10, 1); 
 
//////..... 
 
........

答えて

0

あなたはこのような何かを行うことができます。以下のサンプルコード

public void getQtyReserved() { 
 
     JsonObjectRequest request_json = new JsonObjectRequest(Request.Method.GET, cart_url + name, null, 
 
       new Response.Listener<JSONObject>() { 
 
        @Override 
 
        public void onResponse(JSONObject response) { 
 
         listMap = new HashMap<Integer, Integer>(); 
 
         try { 
 
          JSONArray ja_data = response.getJSONArray("list"); 
 
          int length = ja_data.length(); 
 

 

 
          for (int i = 0; i < length; i++) { 
 
           JSONObject jsonobject = ja_data.getJSONObject(i); 
 

 
           int quantity_reserved = jsonobject.getInt("quantity_reserved"); 
 
           int inventory_id = jsonobject.getInt("inventory_id"); 
 

 
        listMap.put(inventory_id, quantity_reserved); 
 
          }              
 

 
         } catch (JSONException e) { 
 
          e.printStackTrace(); 
 
         } 
 
        } 
 
       }, new Response.ErrorListener() { 
 
      @Override 
 
      public void onErrorResponse(VolleyError error) { 
 
       VolleyLog.e("Error: ", error.getMessage()); 
 
       Toast.makeText(getApplicationContext(), "its empty", Toast.LENGTH_LONG).show(); 
 
      } 
 
     }); 
 
     MySingleton.mySingletonInstance(getApplicationContext()).addToRequestque(request_json); 
 
    }

+1

はあなたの時間をありがとうございました、私はそれを感謝しています。 – Rishfaa

関連する問題