0

我正在尝试从服务器获取一些数据,但我的列表仍然为空,据我所知,问题是未调用 onPersonse()

这是我的辛格顿课

public class VolleySingleton extends Application {
public static final String TAG = VolleySingleton.class.getSimpleName();
private RequestQueue requestQueue;
private static VolleySingleton instance;

@Override
public void onCreate() {
    super.onCreate();
    instance = this;
}

public static synchronized VolleySingleton getInstance() {
    return instance;
}

public RequestQueue getRequestQueue() {
    if (requestQueue == null) {
        requestQueue = Volley.newRequestQueue(getApplicationContext());
    }
    return requestQueue;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> request) {
    getRequestQueue().add(request);
}

public void cancelPendingRequests(Object tag) {
    if (requestQueue != null) {
        requestQueue.cancelAll(tag);
    }
}

}

以及我试图从服务器获取数据的方法

private static String TAG = MainActivity.class.getSimpleName();
public static void getRoomArray(String url, final List<Room> roomList, final Context context) {
    JsonArrayRequest arrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Log.d(TAG, response.toString());
            try {
                Room room = null;
                for (int i = 0; i < response.length(); i++) {
                    room=new Room();
                    JSONObject newRoom = (JSONObject) response.get(i);
                    room.setId(newRoom.getLong("id"));
                    room.setName(newRoom.getString("name"));
                    room.setActive(newRoom.getBoolean("active"));
                    roomList.add(room);
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(context,"Error During Load Data",Toast.LENGTH_SHORT).show();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Toast.makeText(context,volleyError.getMessage(),Toast.LENGTH_SHORT).show();
        }
    });
    VolleySingleton.getInstance().addToRequestQueue(arrayRequest);
}
4

0 回答 0