我想获取和显示数据,但它不能正常工作并在此行之后
var response = await client.GetAsync(uri).ConfigureAwait(false);
代码从未执行过甚至不抛出异常
public async Task<List<Restresponse>> RefreshDataAsync()
{
var Items = new List<Restresponse>();
try
{
HttpClient client = new HttpClient();
var uri = new Uri(string.Format("https://jsonplaceholder.typicode.com/posts", string.Empty));
var response = await client.GetAsync(uri).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); ;
// Items = JsonConvert.DeserializeObject<List<TodoItem>>(content);
}
}
catch (Exception e)
{
Log.WriteLine(LogPriority.Debug, "EXception from restservice", e.Message);
}
return Items;
}
我在片段
中的OncreateView中调用了这个函数public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.recycleview, container, false);
mRecyclerView = view.FindViewById<RecyclerView>(Resource.Id.recyclerView);
mLayoutManager = new LinearLayoutManager(this.Activity);
mRecyclerView.SetLayoutManager(mLayoutManager);
// Prepare the data source:
listItem = new List<Restresponse>();
try
{
listItem = RefreshDataAsync().Result;
// Instantiate the adapter and pass in its data source:
mAdapter = new RecycleViewAdapter(listItem, this.Activity);
// Plug the adapter into the RecyclerView:
mRecyclerView.SetAdapter(mAdapter);
}
catch (System.Exception e)
{
Log.WriteLine(LogPriority.Debug, "EXception from fragmenbt", e.Message);
}
return view;
}
答案 0 :(得分:2)
当然你遇到了僵局。
如果您是从构造函数或事件RefreshDataAsync().Result;
进行调用,则应从函数中删除.ConfigureAwait(false);
,因为如果您在主线程中它将永远不会完成。
评论后的另一个选项 我认为如果您需要异步处理,最好的方法是在创建视图后使用OnResume。
public async override void OnResume()
{
base.OnResume();
try
{
listItem = RefreshDataAsync().Result;
// Instantiate the adapter and pass in its data source:
mAdapter = new RecycleViewAdapter(listItem, this.Activity);
// Plug the adapter into the RecyclerView:
mRecyclerView.SetAdapter(mAdapter);
}
catch (System.Exception e)
{
Log.WriteLine(LogPriority.Debug, "EXception from fragmenbt", e.Message);
}
}
不要忘记从调用RefreshDataAsync()的OnCreateView中删除Try / catch块
完整代码片段:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
try
{
View view = inflater.Inflate(Resource.Layout.recycleview, container, false);
mRecyclerView = view.FindViewById<RecyclerView>(Resource.Id.recyclerView);
mLayoutManager = new LinearLayoutManager(this.Activity);
mRecyclerView.SetLayoutManager(mLayoutManager);
}
catch (Exception)
{
Log.WriteLine(LogPriority.Debug, "EXception from fragment inside OnCreateView", e.Message);
}
}
public async override void OnResume()
{
base.OnResume();
// Prepare the data source:
try
{
using (var client = new HttpClient())
{
var uri = "jsonplaceholder.typicode.com/posts";
var result = await client.GetStringAsync(uri);
var Restresponse = JsonConvert.DeserializeObject<List<Restresponse>>(result);
mAdapter = new RecycleViewAdapter(Restresponse, this.Activity);
mRecyclerView.SetAdapter(mAdapter);
}
}
catch (System.Exception e)
{
Log.WriteLine(LogPriority.Debug, "EXception from fragment inside OnResume", e.Message);
}
}