【发布时间】: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