I cannot return a LinkedHashMap from method and I

2019-09-05 05:30发布

问题:

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?

回答1:

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