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?