2014-02-14 32 views
0

我試圖加載所有從我的Instagram照片的圖片說明飼料使用上和RestSharp一個Xamarin.iOS表,但每當我嘗試運行我的代碼,我總是得到這個錯誤:無法在表加載JSON瓦特/ RestSharp

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的圖片創建的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 { 公共列表數據{獲得;組; } 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; 
    } 
} 

對不起,太長的問題!並感謝您的幫助!

回答

0

你告訴它有5個表中的行,不管你實際有多少數據。由於您正在加載數據異步,第一次加載表時沒有數據。改變這個:

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

那麼現在它說'數據'不是一個對象的實例。 – Prad

+0

我已編輯的Instagram類,包括我使用 – Prad

+0

檢查數據是否是空的方法第一 - 看到我的編輯 – Jason