【问题标题】:Cannot load JSON on Table w/ RestSharp无法在带有 RestSharp 的表上加载 JSON
【发布时间】:2014-02-15 05:27:12
【问题描述】:

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

这是我尝试加载代码的方式,这是我的要求:

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 图片创建的 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; }
}

最后是 My Table View 的数据源: 公共类 TableSource : UITableViewSource { 公共列表数据 { 获取;放; } 受保护的字符串 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;
    }
}

抱歉,问题太长了!并在此先感谢您的帮助!

【问题讨论】:

    标签: ios json xamarin tableview restsharp


    【解决方案1】:

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

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

    【讨论】:

    • 现在它说“数据”不是对象的实例。
    • 我已经编辑了 Instagram 类以包含我正在使用的方法
    • 那么你需要使用调试器来找出你的数据没有被初始化的原因。此外,您对泛型的使用似乎有点毫无意义——您真的要使用这些具有多种类型的类吗?我不认为这会导致问题,但它会混淆你正在做的事情。
    • 响应字符串(response.Content)中有什么?也许 List 应该是 List 而不是 List 并且您应该传入 response.Data.data?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多