gpt4 book ai didi

android - 具有分页响应的 SyncAdapter 和 Rest Server

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:44:08 28 4
gpt4 key购买 nike

我需要知道如何处理来自 Rest Server 的同步适配器和分页响应。我正在制作一个检索数据集合的 Android 应用程序。每页有 20 个项目,我现在在一个请求中检索我的所有项目。我认为我能做到的最好方法是检索一个页面,例如,滚动到 ListView 的末尾时使用 syncAdapter 发出另一个请求,但我不确定。

我正在搜索如何在 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;
}

最佳答案

使用 SyncAdapter 建议您尝试 this .

也可以使用这个来检测 RecyclerView 中列表的结尾:

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);

// When went to the end of the list, load more posts
if (newState == RecyclerView.SCROLL_STATE_IDLE) {

if (linearLayoutManager.findLastVisibleItemPosition() >= linearLayoutManager.getItemCount() - 1) {

// Grow List
}
}
}

关于android - 具有分页响应的 SyncAdapter 和 Rest Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22743052/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com