在RestSharp中显示下一组图像

时间:2014-06-20 18:43:17

标签: c# ios xamarin.ios xamarin restsharp

我正在尝试在Xamarin中创建一个无限的桌面作为Instagram客户端。然而,当我滚动到屏幕的底部时,它会加载相同的第二页图像。它正常加载第一页然后正常加载第二页,但是当我尝试加载第三页时,它再次加载第二页。我该如何解决这个问题?这就是我试图加载它的方式:

Account instagram = enumerable.First ();
var instagramAccessToken = instagram.Properties ["access_token"].ToString ();

var request = new RestRequest { RootElement = "data", Resource = "/users/self/feed" };
request.AddParameter ("access_token", instagramAccessToken);

var client = new RestClient ("https://api.instagram.com/v1");
client.ExecuteAsync (request, response => {
    var rootObject = JsonConvert.DeserializeObject<RootObject> (response.Content);

    string nextURL = rootObject.pagination.next_url;
    //// Create Next Client
    var requestBack = new RestRequest ();
    var clientBack = new RestClient (nextURL);

    // GET Next Response
    clientBack.ExecuteAsync (requestBack, responseBack => {
        var rootObjectBack = JsonConvert.DeserializeObject<RootObject> (responseBack.Content);
        table.InvokeOnMainThread (() => {
            // Create list that contains newly attained JSON
            List<Datum> instagramData = rootObjectBack.data;
            // Create dummy list for other values
            List<Datum> sloppy = null;
            ((TableSource<Datum>)table.Source).instaData.AddRange (instagramData);
            table.ReloadData ();
            });
        });
    });

这是我检测到用户位于TableView底部的地方

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {

        NetworkCheck netCheck = new NetworkCheck ();
        netCheck.runCheck ();
        bool log = netCheck.anyLoggedIn ();

        if (tableView.ContentSize.Height - UIScreen.MainScreen.Bounds.Height <= tableView.ContentOffset.Y) {
            // this is the last row in the last section
            Console.WriteLine ("~~~Bottom~~~");
            FLDTRequest fldt = new FLDTRequest ();
            fldt.getPastInstagramFeed (tableView);
        }
        var cell = tableView.DequeueReusableCell(InstagramCell.Key) as InstagramCell;

        if (cell == null)
        {
            cell = new InstagramCell();
            var views = NSBundle.MainBundle.LoadNib("InstagramCell", cell, null);
            cell = Runtime.GetNSObject(views.ValueAt(0)) as InstagramCell;
        }

        Datum h = instaData [indexPath.Row];
        cell.BindData(h);

        return cell;
    }

1 个答案:

答案 0 :(得分:1)

将回调放在一个单独的递归函数中,只要有一个nexturl就会一直调用它。不知道Instagram API,但这样的事情应该有效。

在viewdidload或其他东西中运行(不是每次到达底部但只有一次)

nextUrl = null;

Account instagram = enumerable.First ();
var instagramAccessToken = instagram.Properties ["access_token"].ToString ();

var request = new RestRequest { RootElement = "data", Resource = "/users/self/feed" };
request.AddParameter ("access_token", instagramAccessToken);

var client = new RestClient ("https://api.instagram.com/v1");
client.ExecuteAsync (request, resp =>
    {
        PageRetrievedCallback(resp );
    });

回调功能

将nextUrl存储为类值

string nextUrl = null;

public void PageRetrievedCallback(RestResponse response)
{
    var rootObject = JsonConvert.DeserializeObject<RootObject> (response.Content);
    nextURL = rootObject.pagination.next_url;

    table.InvokeOnMainThread (() => {
            // Create list that contains newly attained JSON
            List<Datum> instagramData = rootObject.data;
            // Create dummy list for other values
            List<Datum> sloppy = null;
            ((TableSource<Datum>)table.Source).instaData.AddRange (instagramData);
            table.ReloadData ();
         });
}

当你检测到底部加载下一页时运行它:

if ( ! string.IsNullOrEmpty (nextURL) ) 
{

    var requestBack = new RestRequest ();
    var clientBack = new RestClient (nextURL);
    clientBack.ExecuteAsync (requestBack, resp =>
    {
        PageRetrievedCallback(resp );
    });
}