0

I use a method to get with volley my json object from server. The part that receives my object works fine, I get my response and I try to put my values into a linkedhashmap. When I try to return my map I get null.

My method is like:

 public static  LinkedHashMap<String,ArrayList<String>>  GetBusinessInCategories() 

I declare my linkedhashmap outside method like:

final static  LinkedHashMap<String,ArrayList<String>> myMap = new LinkedHashMap<String,ArrayList<String>>();


public static  LinkedHashMap<String,ArrayList<String>>  GetBusinessInCategories() {
    String tag_string_req = "req_register";
    //final LinkedHashMap<String,ArrayList<String>> myMap = new LinkedHashMap<String,ArrayList<String>>();
    //showDialog();

    // prepare the Request
    StringRequest getRequest = new StringRequest(Request.Method.GET, URL_GET_BUSINESS_CATS, new Response.Listener<String>()
    {
                @Override
                public void onResponse(String response)
                {
                    String MyResponse = fixEncodingUnicode(response);
                    JSONObject j = null;
                    JSONArray result;

                    ArrayList listData = new ArrayList<String>();
                    ArrayList NewlistData = new ArrayList<String>();

                    try {
                        j = new JSONObject(MyResponse);

                        JSONArray jarray = j.getJSONArray("List");
                        for (int hk = 0; hk < jarray.length(); hk++) {
                            JSONObject d = jarray.getJSONObject(hk);
                            // Adding Header data
                            JSONObject obj_cat = d.getJSONObject("Category");
                            JSONObject obj_bus = d.getJSONObject("Business");
                            String myKey = obj_cat.getString("cat_id");
                            String myVal = obj_bus.getString("Name");

                            if(myMap.containsKey(myKey))
                            {
                                listData.add(myVal);
                                ArrayList MergeNewlistData = new ArrayList<String>();
                                MergeNewlistData.addAll(listData);
                                MergeNewlistData.addAll(NewlistData);
                                myMap.put(myKey,MergeNewlistData);

                            }else
                            {
                                    NewlistData = new ArrayList<String>();
                                    NewlistData.add(myVal);
                                    myMap.put(myKey,NewlistData);
                            }

                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    // display response
                    //Log.d("Response", mympap.toString());
                    // display response
                    Log.d("Response", MyResponse.toString());

                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Error", "Registration Error: " + error.getMessage());
                    Toast.makeText(context, error.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
    );
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(getRequest, tag_string_req);
    return myMap;

}

What I am doing wrong here?

  • Can you show the complete code please? – Pravin Sonawane Nov 24 '16 at 18:15
  • Your code has incorrect flow for working with asynchronous callbacks. onResponse(), onError() these do not get called when you instantiate your StringRequest object. So, when you call GetBusinessInCategories(), it just sets up the callback methods like onResponse(). It doesn't mean that it is executed and you have your myMap updated. Whenever the response comes back asynchronously, your map object will be updated. But, your GetBusinessInCategories() method returned(synchronously) an empty map object. So, I recommend you to take actions after the response arrives in onResponse() callback – Hungry Coder Nov 24 '16 at 18:36
  • Well nice explanation but its quite blur for me and I am quite novice , You mean to create myMap after onResponse Callback? –  Nov 24 '16 at 18:45

1 Answers1

0

When I try to return my map I get null. What I am doing wrong here?

You can't have your method return the map. Just make it void.

If you need to use the map, declare a method that accepts it as a parameter, pass the map through at the very end of onResponse within the Volley listener, and continue on with building an adapter and such within that new method.

That's how you are supposed to use asynchronous methods - in other words, you're getting null immediately returned while the Volley code is off in the background doing something else

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • A simple method like public LinkedHashMap MyMethod( LinkedHashMap map){ return map;} something like this? –  Nov 25 '16 at 07:39
  • Um no. Like build an adapter with the map parameter. Set said adapter to a listView. Things that you would normally do if you were able to return this map as you'd written. – OneCricketeer Nov 25 '16 at 07:41
  • I have already the adapter of expandable listview so you mean to using it to set my listview inside my volley method?, if you could provide an example please! –  Nov 25 '16 at 07:51
  • You already have an adapter? You need to give an Arraylist when you declare that, right? So why are you making a new list within the Volley code instead of adding to the adapters list? Alternatively, make a new adapter, like I said, at the end of the onResponse using the map... Also, you want to notifyDatasetChanged on the adapter at the end of whatever code you use to update the adapter. – OneCricketeer Nov 25 '16 at 08:01
  • I have my request methods in a separate class but I think I get what you mean, I 'll try it –  Nov 25 '16 at 08:14
  • If you move `Response.Listener` as a parameter of `GetBusinessInCategories` it'll be easier to get your JSON data back to where you need it – OneCricketeer Nov 25 '16 at 08:16