我需要建议是这个解决方案可以接受并且不会导致溢出,我更新了使用AsyncTask读取的数据,在AsyncTask完成后我需要一次又一次地更新。 这个解决方案是否可接受且安全
private class DownloadFilesTask extends AsyncTask<URL,Integer,com.ring_view.www.json.System> { @Override protected com.ring_view.www.json.System doInBackground(URL... params) { int count = params.length; URL temp=params[0]; System system=null; try { system = Communicator.getSystem(temp); } catch (LoggingConnectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONParsingErrorException e) { // TODO Auto-generated catch block e.printStackTrace(); } return system; } protected void onProgressUpdate(Integer... progress) { //setProgressPercent(progress[0]); } protected void onPostExecute(com.ring_view.www.json.System result) { txtWorkAllowedValue.setText(result.work_allowed); try { new DownloadFilesTask().execute(new URL("http://test/status-system.json")); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }我第一次调用new DownloadFilesTask()。execute(new URL(“http://test/status-system.json”)); 在OvCreate方法中,它在模拟器中工作正常。 这是安全还是有更优雅的解决方案?
I need advice is this solution acceptable and not cause overflow, I update data which read with AsyncTask, after AsyncTask finished I need to update again and again. Is this solution acceptable and safe
private class DownloadFilesTask extends AsyncTask<URL,Integer,com.ring_view.www.json.System> { @Override protected com.ring_view.www.json.System doInBackground(URL... params) { int count = params.length; URL temp=params[0]; System system=null; try { system = Communicator.getSystem(temp); } catch (LoggingConnectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONParsingErrorException e) { // TODO Auto-generated catch block e.printStackTrace(); } return system; } protected void onProgressUpdate(Integer... progress) { //setProgressPercent(progress[0]); } protected void onPostExecute(com.ring_view.www.json.System result) { txtWorkAllowedValue.setText(result.work_allowed); try { new DownloadFilesTask().execute(new URL("http://test/status-system.json")); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }I first time call new DownloadFilesTask().execute(new URL("http://test/status-system.json")); in OvCreate method and it works fine in emulator. Is this safe or there is some more elegant solution ?
最满意答案
是的,多次实例化AsyncTask是可以接受的,例如......
new DownloadFilesTask().execute(...); ... new DownloadFilesTask().execute(...);...被允许。
你不能做类似以下的事情......
DownloadFilesTask myTask = new DownloadFilesTask(); myTask.execute(...); // This is OK myTask.execute(...); // This will cause an exception这是因为两次执行相同的线程是不合法的。 在第一个示例中,使用new重复为doInBackground(...)创建一个新线程,但在第二个示例中,它尝试重新使用前一个。
Yes, instantiating an AsyncTask multiple times is acceptable, example...
new DownloadFilesTask().execute(...); ... new DownloadFilesTask().execute(...);...is allowed.
You must not do something like the following though...
DownloadFilesTask myTask = new DownloadFilesTask(); myTask.execute(...); // This is OK myTask.execute(...); // This will cause an exceptionThis is because it isn't legal to execute the same thread twice. In the first example using new repeatedly creates a new thread for doInBackground(...) but in the second example it is trying to re-use the previous one.
更多推荐
发布评论