2 回答
TA贡献1793条经验 获得超6个赞
如果您的响应返回一个带有城市元素数组的 JSONObject,那么您的请求是正确的,但如上所述,在调用回调之前,您不能期望实际得到结果。这是一个使用返回 JSONArray 的 JSONArrayRequest 的示例,但在您从 JSONObject 中提取 City 数组后,其工作方式与您的几乎相同。
private ListView mTripList;
public ArrayList<TripItem> tripItems;
private JSONArray unclaimedTrips;
private TripSelectAdapter adapter;
public void getTrips() {
JsonArrayRequest req = new JsonArrayRequest(url, new Response.Listener<JSONArray> () {
@Override
public void onResponse(JSONArray response) {
// Public Array
unclaimedTrips = response;
tripItems = new ArrayList<TripItem>();
// Optional if you want to manipulate the data in some way
if (unclaimedTrips != null) {
for (int i = 0; i < unclaimedTrips.length(); i++) {
try {
JSONObject item = unclaimedTrips.getJSONObject(i);
int tripID = item.getInt("trip_id");
int claimID = item.getInt("claim_id");
String payrollCode = item.getString("payroll_code");
String tripDate = item.getString("trip_date");
String tripPurpose = item.getString("trip_purpose");
TripItem tripItem = new TripItem(tripID, claimID, payrollCode, tripDate, tripPurpose);
tripItems.add(tripItem);
} catch (JSONException e) {
e.printStackTrace();
}
}
refreshData();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
unclaimedTrips = null;
refreshData();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Accept", "application/json");
headers.put("Content-Type", "application/json");
return headers;
};
};
// add the request object to the queue to be executed
MyApplication.getInstance().addToRequestQueue(req);
}
public void refreshData() {
if (tripItems.isEmpty()) {
mEmptyList.setVisibility(android.view.View.VISIBLE);
} else {
mEmptyList.setVisibility(android.view.View.GONE);
}
adapter = new TripSelectAdapter(this, thisContext, tripItems);
mTripList.setAdapter(adapter);
}
TA贡献1851条经验 获得超3个赞
添加回答
举报