SyncAdapter和Rest Server具有分页响应

时间:2014-03-30 10:50:26

标签: android rest android-syncadapter

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

我正在搜索如何在REST中使用分页处理android但我没有找到任何有用的东西。我想知道是否有人可以帮助我。

谢谢。

以下是我现在如何检索项目的示例。

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;
}

1 个答案:

答案 0 :(得分:0)

使用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
                    }
                }
}