2016-09-13 31 views
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

你可以处理的数据完成了工作。 您可以在此处设置回调以在班级中发送回复。

+0

我该怎么办呢? –

+0

1.创建接口 “公共接口OnResponseReceived { 公共无效responseReceived(JSONArray响应); }” 2.Implement这个接口在活动 3.当调用排球方法 “getRoomArray()” 通过实例您的活动 4.Now你可以调用方法“responseReceived(jSONArray),其中”中凌空响应和传递数组到methos.So您将获得该数组中的活动 – rushi

+0

是什么downvote以上回答的原因是什么? – rushi

相关问题