我在Android中遇到Volley请求的问题,我正在尝试从服务器中获取学生的蝙蝠侠,然后获取与每个batchno详细信息相对应的学生详细信息。我的日志显示来自服务器的响应,但它们都是随机顺序。此外,响应在recyclerview中以随机顺序设置。 这是批量和学生的排球和json:
private void setData() {
students = new ArrayList<>();
StringRequest batchRequest=new StringRequest(Request.Method.POST, BATCH_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
parseBatchJsonResponse(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("ResponseError",""+error.toString());
}
});
VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(studentRequest,"req");
}
private void parseBatchJsonResponse(String response) throws JSONException {
Log.d("Resp",""+response);
JSONObject batchObject=new JSONObject(response);
JSONArray batchArray=batchObject.getJSONArray("result");
for (int i=0;i<batchArray.length();i++){
JSONObject bObject=batchArray.getJSONObject(i);
batchNos=bObject.getString("batch");
Log.d("batchNo",""+batchNos);
getStudentData(batchNos);
}
}
private void getStudentData(String batchNo) {
batchNos =batchNo= "\"" + batchNo + "\"";
String studentURl=STUDENT_URL+batchNo.trim();
Log.d("SURL",""+studentURl);
StringRequest studentRequest =new StringRequest(Request.Method.GET,studentURl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Log.d("Student",""+response+batchNos);
parseStudentJson(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(studentRequest,"req");
}
private void parseStudentJson(String batchNo) throws JSONException {
// students.clear();
JSONObject studentOb=new JSONObject(batchNo);
JSONArray stuArray=studentOb.getJSONArray("result");
for (int i=0;i<stuArray.length();i++){
JSONObject stu=stuArray.getJSONObject(i);
String stuName=stu.getString("student");
students.add(new Student(stuName,100));
Log.d("BNO",""+batchNos);
}
batches.add(new Batch(batchNos,students));
adapter.notifyDataSetChanged();
答案 0 :(得分:0)
不要每次都有新请求队列。请单个请求队列。
public class VolleySingleton {
private static VolleySingleton mAppSingletonInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mContext;
private Request.Priority Low;
private VolleySingleton(Context context) {
mContext = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,new ImageLoader.ImageCache()
{
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized VolleySingleton getInstance(Context context) {
if (mAppSingletonInstance == null) {
mAppSingletonInstance = new VolleySingleton(context);
}
return mAppSingletonInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(tag);
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
然后
VolleySingleton.getInstance(context).addToRequestQueue(jsonObjectRequest,tag);