我的volley
函数并未返回实际hashmap
个我需要的项目。
从PHP文件获取JSON对象的函数,获取对象,正确解析它,但我最初声明的hashmap
未正确创建。
所以经过研究我意识到我需要为我的排球做一个回调,我找到了一些教程,但是我仍然无法做到这一点,如果有人可以帮助一个新手,我将非常感激......谢谢多!
public HashMap< String,Integer> makeHashMap(final Context C,){
final HashMap< String,Integer> hm = new HashMap< String,Integer>(); // intilaizaiton to empty hashmap
final String httpUpdateRoutines = "some php file here ";
RequestQueue queue = Volley.newRequestQueue(C);
StringRequest request = new StringRequest(Request.Method.POST, httpUpdateRoutines, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONArray dataJsonArr = new JSONArray(response);
// loop through all users
for(int i=0;i<dataJsonArr.length();i++){
JSONObject c=dataJsonArr.getJSONObject(i);
String name=c.getString("name");
hm.put(name,i+1); // fill hashmap
reversedHashMap.put(i+1, name); //ignore this declear elsewhere
}
Log.e("sizeofhasmapinrequest", String.valueOf(hm.size()));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(C, "Data Not Published " + error, Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams () throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("type","query");
return map;
}
};
queue.add(request);
Log.e("sizeofhashmapbyrequest2", String.valueOf(hm.size())); // returns 0
return hm;
}
&#13;