Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. private final ScheduledExecutorService downloadScheduler = Executors.newSingleThreadScheduledExecutor(1);
  2.  
  3. final Runnable scoreHttpRunnable = new Runnable() {
  4. @Override public void run() {
  5. ...
  6. //do Http Syncronously here- I guess whatever is in the doInBackground(...) part of that Async task you wrote!
  7. ...
  8. final int newScoreResult = ... (do whatever you do here)
  9. ...
  10. runOnUiThread(new Runnable() { @Override public void run() { yourView.updateHoweverYouLike(newScoreResult); } })
  11. ...
  12. };
  13. downloadScheduler.scheduleAtFixedRate(scoreHttpRunnable, 0, 4, TimeUnit.SECONDS);
  14.  
  15. AsyncTasks should ideally be used for short operations (a few seconds at the most.)
  16.  
  17. @Override
  18. protected String doInBackground(String... params) {
  19. Log.d(TAG, "type - " + params[0] + ", url = " + params[1] + ", name = " + params[2]);
  20.  
  21.  
  22. downloadFile(params[1], params[2]);
  23.  
  24.  
  25. return null;
  26. }
  27.  
  28. URL url = new URI(Url.replace(" ", "%20")).toURL();
  29. URLConnection connection = url.openConnection();
  30. connection.setConnectTimeout(1000);
  31. int fileLength = connection.getContentLength();
  32.  
  33. mSavePath = CommonUtilities.getFileSavePath(mContext, fileName, fileLength);
  34. Log.d(TAG, "*** saveFilePath - " + mSavePath);
  35.  
  36. InputStream inputStream = connection.getInputStream();
  37. if (inputStream != null) {
  38. File file = new File(mSavePath);
  39.  
  40. BufferedOutputStream bufferOutputStream = new BufferedOutputStream(new FileOutputStream(file));
  41. byte byteArray[] = new byte[1024];
  42. int len = 0;
  43. long total = 0;
  44.  
  45. while ((len = inputStream.read(byteArray)) != -1) {
  46. bufferOutputStream.write(byteArray, 0, len);
  47. total += len;
  48. }
  49.  
  50. bufferOutputStream.flush();
  51. bufferOutputStream.close();
  52. inputStream.close();
  53. } else {
  54. Log.d(TAG, "*** inputStream is null");
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement