I cannot return a LinkedHashMap from method and I get null

杀马特。学长 韩版系。学妹 提交于 2019-12-12 03:42:46

问题


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



来源:https://stackoverflow.com/questions/40792534/i-cannot-return-a-linkedhashmap-from-method-and-i-get-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!