gpt4 book ai didi

c# - Instagram 到带有 RestSharp 的 TableView

转载 作者:行者123 更新时间:2023-11-29 03:13:31 26 4
gpt4 key购买 nike

您好,我正在尝试使用 Xamarin.iOS 将我的 Instagram 提要中的个人姓名加载到 UITableView 中。但是它只显示类的名称,您可以在此处看到:

Simulator Screenshot

这是我用来尝试提取 Instagram 提要的请求:

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<List<RootObject>>(request, response => {
Console.WriteLine(response.Content);
Console.WriteLine(response.Data);
InvokeOnMainThread(delegate {
// pass the data to the TableSource class
((TableSource<RootObject>)table.Source).Data = response.Data;

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

这是传入 Instagram Feed 的类文件:

    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 override string ToString ()
{
return string.Format ("[User: username={0}, website={1}, profile_picture={2}, full_name={3}, bio={4}, id={5}]", username, website, profile_picture, full_name, bio, id);
}
}

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类下的getCell方法:

    public class TableSource<RootObject> : UITableViewSource 
{
public List<RootObject> Data { get; set; }
protected string cellIdentifier = "TableCell";

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

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

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

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].data[0].user.username;

return cell;
}
}

最佳答案

您正在传递 List<RootObject>到您的表源。每个 RootObject 都有一个数据属性,即 List<Datum> ;每个 Datum 都有一个 User 属性,每个 User 都有一个 username 属性。

所以你想做这样的事情(我是任意抓取第一个 Datum,这可能不正确)

cell.TextLabel.Text = Data[indexPath.Row].data[0].user.username;

关于c# - Instagram 到带有 RestSharp 的 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21958212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com