使用 httpClient 获取对 github 存储库的请求?
Get request to github repos using httpClient?
我正在尝试从 api 回购端点获取回购列表,但是我在用方法实现它时遇到了问题。我得到的错误指出它无法将 IEnumerable RepoData 转换为 IEnumerable RepoData。这是我目前所拥有的:
public class RepoResult
{
public IEnumerable<RepoData> RepoData { get; set; }
}
public class RepoData
{
public int Id { get; set; }
public string Name { get; set; }
public string Html_Url { get; set; }
}
public async Task<IEnumerabe<RepoData>> LoadRepos()
{
string url = "https://api.github.com/users/jeremyolu/repos";
using (HttpResponseMessage response = await _apiClient.GetAsync(url))
{
if (response.IsSuccessStatusCode)
{
RepoResult result = await response.Content.ReadAsAsync<RepoResult>();
return result.RepoData;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
不要定义 RepoResult class 只需这样做:
if (response.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<IEnumerable<RepoData>>(await response.Content.ReadAsStringAsync());
}
我正在尝试从 api 回购端点获取回购列表,但是我在用方法实现它时遇到了问题。我得到的错误指出它无法将 IEnumerable RepoData 转换为 IEnumerable RepoData。这是我目前所拥有的:
public class RepoResult
{
public IEnumerable<RepoData> RepoData { get; set; }
}
public class RepoData
{
public int Id { get; set; }
public string Name { get; set; }
public string Html_Url { get; set; }
}
public async Task<IEnumerabe<RepoData>> LoadRepos()
{
string url = "https://api.github.com/users/jeremyolu/repos";
using (HttpResponseMessage response = await _apiClient.GetAsync(url))
{
if (response.IsSuccessStatusCode)
{
RepoResult result = await response.Content.ReadAsAsync<RepoResult>();
return result.RepoData;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
不要定义 RepoResult class 只需这样做:
if (response.IsSuccessStatusCode)
{
return JsonConvert.DeserializeObject<IEnumerable<RepoData>>(await response.Content.ReadAsStringAsync());
}