我不能 return 来自方法的 LinkedHashMap,我得到 null
I cannot return a LinkedHashMap from method and I get null
我使用一种方法从服务器截取我的 json 对象。接收我的对象的部分工作正常,我得到了我的回应,我尝试将我的值放入 linkedhashmap 中。当我尝试 return 我的地图 我得到 null。
我的方法是这样的:
public static LinkedHashMap<String,ArrayList<String>> GetBusinessInCategories()
我声明我的 linkedhashmap 外部方法如下:
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;
}
我这里做错了什么?
When I try to return my map I get null. What I am doing wrong here?
您的方法 return 无法使用地图。就让它作废吧。
如果您需要使用地图,声明一个接受它作为参数的方法,在 Volley 侦听器中的 onResponse 的最后传递地图,并继续在新的中构建适配器等方法。
这就是您应该使用异步方法的方式 - 换句话说,当 Volley 代码在后台关闭执行其他操作时,您会立即得到 null returned
我使用一种方法从服务器截取我的 json 对象。接收我的对象的部分工作正常,我得到了我的回应,我尝试将我的值放入 linkedhashmap 中。当我尝试 return 我的地图 我得到 null。
我的方法是这样的:
public static LinkedHashMap<String,ArrayList<String>> GetBusinessInCategories()
我声明我的 linkedhashmap 外部方法如下:
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;
}
我这里做错了什么?
When I try to return my map I get null. What I am doing wrong here?
您的方法 return 无法使用地图。就让它作废吧。
如果您需要使用地图,声明一个接受它作为参数的方法,在 Volley 侦听器中的 onResponse 的最后传递地图,并继续在新的中构建适配器等方法。
这就是您应该使用异步方法的方式 - 换句话说,当 Volley 代码在后台关闭执行其他操作时,您会立即得到 null returned