这是我的第一个网址
http://www.adayalangal.com/wp-json/wp/v2/posts/?page=1
那里最多有8页...... `
JsonArrayRequest productReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, String.valueOf(response));
System.out.print(response);
pDialog.hide();
JSONObject obj;
DataModel dataModel;
JSONObject uniObject;
//code for retreiving web content
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
//hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(productReq);`
当我运行这个时,我只得到第一页的内容...我想要检索内容,直到来自网址的响应变为空...我怎么能实现这个
答案 0 :(得分:1)
为api调用创建单独的方法,如下所示:
定义一个检查前一个响应是否为空的变量
boolean isPreviousResponseWasEmpty = false;
private void getData(String pageNumber){
String url = url + pageNumber ;
JsonArrayRequest productReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, String.valueOf(response));
System.out.print(response);
pDialog.hide();
parseData(response.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
//hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(productReq);`
}
创建单独的方法来解析您的服务器响应:
private void parseData(String serverResponse){
if (serverResponse!=null && !serverResponse.equals("")){
JSONObject obj;
DataModel dataModel;
JSONObject uniObject;
//code for retreiving web content
}else{
if (!isPreviousResponseWasEmpty){
isPreviousResponseWasEmpty = false;
getData(incrementedPageNumber);
}else{
isPreviousResponseWasEmpty = true;
}
}
}