解析器没问题但是当我把它放到asynctask的上下文中时它抛出了IOE异常。我该怎么办?
{
customer:
{
name: 'james'
email: 'j@sample.com'
}
}
同时我有堆栈跟踪:
.navbar-default {
border: 0;
}
我想要做的是更新选项卡式活动中的列表片段?没有失败......我得到了
private class AsyncCaller extends AsyncTask<Void, Void, List<DummyContent.DummyItem>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// This method will be running on UI thread
}
@Override
protected List<DummyContent.DummyItem> doInBackground(Void... params) {
// This method will be running on background thread so don't update UI from here
// Do your long running http tasks here and you can access the parent class' variable url over here
List<DummyContent.DummyItem> list = new ArrayList<>();
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
DummyContent.UNAME, DummyContent.PASSWORD.toCharArray());
}
});
try {
DummyContentRegistry registry = new XMLDummyContentParser(DummyContent.url).parse();
for (DummyContent.DummyItem t: registry.getTeachers()) {
list.add(t);
}
} catch (Exception e) {
e.printStackTrace();
fail("Exception should not have been thrown");
}
return list;
}
@Override
protected void onPostExecute(List<DummyContent.DummyItem> result) {
super.onPostExecute(result);
// This method will be running on UI thread
for (int i = 1; i <= DummyContent.COUNT; i++) {
DummyContent.addItem(DummyContent.createDummyItem(i));
}
for (DummyContent.DummyItem t: result) {
DummyContent.addItem(t);
}
}
}
答案 0 :(得分:1)
它不像我之前想的那样,似乎是连接问题,验证您的互联网状态,互联网权限
最佳做法:
Thread
进行REST呼叫以及长时间运行的任务,并使用AsynchTasks
进行小型任务(下载歌曲,图像处理,大小约5-10 MB)或成熟使用开发了像Volley,Retrofit,(毕加索,滑翔图像)等图书馆。