我的异步调用者出了什么问题?

时间:2016-08-08 05:12:18

标签: java android android-studio

解析器没问题但是当我把它放到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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

它不像我之前想的那样,似乎是连接问题,验证您的互联网状态,互联网权限

最佳做法:

  • 应该为marshmallow及以上版本实现运行时权限请求模型(确保对Build版本使用条件检查并相应地使用代码)
  • 实施连接检查以验证互联网连接(使用NetworkInfo和ConnectivityManager)
  • 为避免崩溃,请始终使用Thread进行REST呼叫以及长时间运行的任务,并使用AsynchTasks进行小型任务(下载歌曲,图像处理,大小约5-10 MB)或成熟使用开发了像Volley,Retrofit,(毕加索,滑翔图像)等图书馆。