2014-03-30 7 views
6

Ich muss wissen, wie man mit einem Syncadapter und einer paginierten Antwort von Rest Server umgeht. Ich mache eine Android-App, die Datensammlung abruft. Jede Seite enthält 20 Artikel und ich erhalte jetzt alle meine Artikel in einer Anfrage. Ich denke, dass der beste Weg, dass ich es tun kann, eine Seite abrufen und wenn zum Beispiel bis zum Ende der Listenansicht scrollen, eine weitere Anfrage mit dem SyncAdapter, aber ich bin mir nicht sicher.SyncAdapter und Rest Server mit paginierter Antwort

Ich suchte, wie man in android mit Seitenumbruch in REST beschäftigt, aber ich fand nichts nützliches. Ich möchte wissen, ob mir jemand helfen kann.

Vielen Dank.

Hier ist ein Beispiel, wie ich Artikel jetzt abrufen.

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

Antwort

0

für die Verwendung von SyncAdapter vorgeschlagen, versuchen Sie this.

auch für das Ende der Liste in RecyclerView erkennt dies mit:

@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 
        } 
       } 
} 
+0

i seiner glaube nicht, dass eine gute Option für SyncAdapter paginierte Antwort zu verwenden. Auch haben Sie über Syncadapter und Scroll-Listener separat gepostet, was kein konkretes Beispiel oder Lead ist – Raghunandan