我需要了解如何处理来自Rest服务器的分页响应和同步适配器。我正在制作一个Android应用程序,用于检索数据集合。每一页都有20个项目,我现在正在一次性检索所有项目。我认为最好的方法是一次检索一页,例如滚动到列表视图的末尾,再使用同步适配器进行另一个请求,但我不确定。
我正在搜索如何在Android中处理REST中的分页,但我没有找到任何有用的信息。我想知道是否有人可以帮助我。
谢谢。
这是我现在如何检索项目的示例。
我正在搜索如何在Android中处理REST中的分页,但我没有找到任何有用的信息。我想知道是否有人可以帮助我。
谢谢。
这是我现在如何检索项目的示例。
public ArrayList<ContentProviderOperation> parse(String json) throws IOException, NullPointerException {
final ArrayList<ContentProviderOperation> batch = new ArrayList<ContentProviderOperation>();
AccountManager manager = AccountManager.get(mContext);
Account account = ((KipptApplication)mContext.getApplicationContext()).getCurrentAccount();
String authToken = manager.peekAuthToken(account, AuthenticatorActivity.PARAM_AUTHTOKEN_TYPE);
Header[] headers = new Header[]{
new BasicHeader(KipptConstants.API_USERNAME_KEY,account.name),
new BasicHeader(KipptConstants.API_TOKEN_KEY,authToken)
};
Gson gson = new Gson();
Type responseType = new TypeToken<Response<ClipObject>>() {}.getType();
Response<ClipObject> inbox = gson.fromJson(json,responseType);
List<ClipObject> clips = inbox.getObjects();
String response = null;
String next = inbox.getMeta().getNext();
while(next !=null){
try {
Log.d(TAG,"Fetching more clips from: " + next);
response = HttpHelper.getHttpResponseAsString(KipptConstants.DOMAIN_URL +
next, null, headers);
inbox = gson.fromJson(response,responseType);
/*Updating next uri*/
next = inbox.getMeta().getNext();
if(!inbox.getObjects().isEmpty()){
clips.addAll(inbox.getObjects());
}else{
Log.d(TAG,"No more clips");
break;
}
} catch (PersonalizedException e) {
Log.e(TAG,e.toString());
e.printStackTrace();
}
}
for(ClipObject clip : clips){
if(mKipptDAO.isClipInDb(mContext.getContentResolver(),clip.getId(),true)== null){
Log.i(TAG,"Adding new clip");
/*Parsing clip*/
parseClip(clip,batch,false /*Clip isn't in database so update=false*/);
/*Parsing media*/
parseMedia(clip.getMedia(),clip.getId(),batch,false);
/*Parsing comments if clip contains it*/
if(clip.getCommentObjects().getCount()>0) {
List<CommentObject> comments = clip.getCommentObjects().getListElements();
for(CommentObject comment: comments){
parseComments(comment,clip.getId(),batch,false);
}
}
/*TODO Parse Likes*/
/*Parsing user creator*/
parseCreator(clip.getUserCreator(),batch,false);
}else{
Log.i(TAG,"Modifying clip");
/*Clip is in database*/
if(!(clip.getUpdated()<= timestamp)){
/*Parsing clip and update it in database*/
parseClip(clip,batch,true);
/*Parsing media and update it in database*/
parseMedia(clip.getMedia(),clip.getId(),batch,true);
/*Parsing comments and update it in database*/
if(clip.getCommentObjects().getCount()>0) {
List<CommentObject> comments = clip.getCommentObjects().getListElements();
for(CommentObject comment: comments){
parseComments(comment,clip.getId(),batch,true);
}
}
/*TODO parse likes*/
/*Parse Creator*/
parseCreator(clip.getUserCreator(),batch,true);
}
}
/*Updating timestamp*/
if(timestamp<=clip.getUpdated())timestamp = clip.getUpdated();
}
/*Saving timestamp modified value in preferences file*/
this.sharedPreferences.edit().putLong(KipptConstants.loadTimeStamp(currentFragmentIndex), timestamp).commit();
return batch;
}