不调用JsonArrayRequest onResponse方法

时间:2016-09-12 10:50:25

标签: android jsonresponse

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

这是我的Singeton课程

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);
}

0 个答案:

没有答案