无法在表w / RestSharp上加载JSON

时间:2014-02-14 21:08:47

标签: ios json xamarin tableview restsharp

我正在尝试使用RestSharp和Xamarin.iOS将Instagram Feed上的图片中的所有字幕加载到表格中,但每当我尝试运行我的代码时,我总会收到此错误:

The Actual Error

以下是我尝试加载代码的方法,这是我的要求:

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

        var client = new RestClient ("https://api.instagram.com/v1/");

        var request = new RestRequest("users/self/feed", Method.GET);
        request.AddParameter("access_token", instagramAccessToken);
        client.ExecuteAsync<List<RootObject>> (request, response => {
            // do work on UI thread
            Console.WriteLine(response.Content);
            InvokeOnMainThread(delegate {
                // pass the data to the TableSource class
                ((TableSource<RootObject>)table.Source).Data = response.Data;

                // make the TableView reload the data
                table.ReloadData();
            });
        });

我已经确认有一个正在运行的访问令牌,并且正在返回一个正在运行的JSON字符串。这是我为Instagram Pictures创建的JSON对象类:

public class InstaPic
    {
        Caption cap = new Caption();
        Pagination pag;
        Meta met;

        public override string ToString()
        {
            return cap.text;
        }
    }

public class Pagination
{
    public string next_url { get; set; }
    public string next_max_id { get; set; }
}

public class Meta
{
    public int code { get; set; }
}

public class Location
{
    public double latitude { get; set; }
    public double longitude { get; set; }
    public string name { get; set; }
    public int? id { get; set; }
}

public class Comments
{
    public int count { get; set; }
    public List<object> data { get; set; }
}

public class Datum2
{
    public string username { get; set; }
    public string profile_picture { get; set; }
    public string id { get; set; }
    public string full_name { get; set; }
}

public class Likes
{
    public int count { get; set; }
    public List<Datum2> data { get; set; }
}

public class LowResolution
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Thumbnail
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class StandardResolution
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Images
{
    public LowResolution low_resolution { get; set; }
    public Thumbnail thumbnail { get; set; }
    public StandardResolution standard_resolution { get; set; }
}

public class From
{
    public string username { get; set; }
    public string profile_picture { get; set; }
    public string id { get; set; }
    public string full_name { get; set; }
}

public class Caption
{
    public string created_time { get; set; }
    public string text { get; set; }
    public From from { get; set; }
    public string id { get; set; }
}

public class User
{
    public string username { get; set; }
    public string website { get; set; }
    public string profile_picture { get; set; }
    public string full_name { get; set; }
    public string bio { get; set; }
    public string id { get; set; }
}

public class LowResolution2
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class StandardResolution2
{
    public string url { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Videos
{
    public LowResolution2 low_resolution { get; set; }
    public StandardResolution2 standard_resolution { get; set; }
}

public class Datum
{
    public object attribution { get; set; }
    public List<object> tags { get; set; }
    public string type { get; set; }
    public Location location { get; set; }
    public Comments comments { get; set; }
    public string filter { get; set; }
    public string created_time { get; set; }
    public string link { get; set; }
    public Likes likes { get; set; }
    public Images images { get; set; }
    public List<object> users_in_photo { get; set; }
    public Caption caption { get; set; }
    public bool user_has_liked { get; set; }
    public string id { get; set; }
    public User user { get; set; }
    public Videos videos { get; set; }
}

public class RootObject
{
    public Pagination pagination { get; set; }
    public Meta meta { get; set; }
    public List<Datum> data { get; set; }
}

最后,这是我的表视图的数据源:     公共类TableSource:UITableViewSource     {         公共列表数据{get;组; }         protected string cellIdentifier =“TableCell”;

    public TableSource ()
    {
        Data = new List<T> ();
    }

    public TableSource(List<T> data)
    {
        Data = data;
    }

    /// <summary>
    /// Called by the TableView to determine how many cells to create for that particular section.
    /// </summary>
    public override int RowsInSection (UITableView tableview, int section)
    {
        return 5;
    }

    /// <summary>
    /// Called when a row is touched
    /// </summary>
    public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
    {
        if (OnRowSelected != null) {
            OnRowSelected (this, new RowSelectedEventArgs (tableView, indexPath));
        }
    }

    public class RowSelectedEventArgs : EventArgs
    {
        public UITableView tableView { get; set; }
        public NSIndexPath indexPath { get; set; }

        public RowSelectedEventArgs(UITableView tableView, NSIndexPath indexPath) : base()
        { 
            this.tableView = tableView;
            this.indexPath = indexPath;
        }
    }

    public event EventHandler<RowSelectedEventArgs> OnRowSelected;

    /// <summary>
    /// Called by the TableView to get the actual UITableViewCell to render for the particular row
    /// </summary>
    public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {
        // request a recycled cell to save memory
        UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
        // if there are no cells to reuse, create a new one
        if (cell == null)
            cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);

        cell.TextLabel.Text = Data[indexPath.Row].ToString();

        return cell;
    }
}

抱歉这个问题太长了!并且感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您告诉它表中有5行,无论您实际拥有多少数据。由于您正在加载数据异步,因此第一次加载表时没有数据。改变这个:

public override int RowsInSection (UITableView tableview, int section)
{
    // return 5;
    if (Data == null) {
      return 0;
    else {
      return Data.Count;
    }
}