1
這裏是我的代碼,我想從DB填單子,然後使用它,當然,但我有問題,它做asychronouz,當我試圖從這個名單中獲得價值,我得到了束縛例外,我應該如何重新調整這個方法,或者我應該如何從Mainactivity調用來解決這個問題?在如何獲得排球完成工作?
public void onResponse(JSONArray response) { //process response }
在onResponse凌空
public class VolleySingleton extends Application {
public static final String TAG = VolleySingleton.class.getSimpleName();
private RequestQueue requestQueue;
private static VolleySingleton instance;
public static int count=0;
@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);
count++;
}
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 = JSonParse.class.getSimpleName();
public static List<Room> getRoomArray(String url, Context context) throws InterruptedException {
final List<Room> roomList = new ArrayList<>();
final CountDownLatch countDownLatch = new CountDownLatch(1);
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);
}
Helper.initList(roomList);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
volleyError.printStackTrace();
}
});
VolleySingleton.getInstance().addToRequestQueue(arrayRequest);
return roomList;
}
我該怎麼辦呢? –
1.創建接口 「公共接口OnResponseReceived { 公共無效responseReceived(JSONArray響應); }」 2.Implement這個接口在活動 3.當調用排球方法 「getRoomArray()」 通過實例您的活動 4.Now你可以調用方法「responseReceived(jSONArray),其中」中凌空響應和傳遞數組到methos.So您將獲得該數組中的活動 – rushi
是什麼downvote以上回答的原因是什麼? – rushi