Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private final ScheduledExecutorService downloadScheduler = Executors.newSingleThreadScheduledExecutor(1);
- final Runnable scoreHttpRunnable = new Runnable() {
- @Override public void run() {
- ...
- //do Http Syncronously here- I guess whatever is in the doInBackground(...) part of that Async task you wrote!
- ...
- final int newScoreResult = ... (do whatever you do here)
- ...
- runOnUiThread(new Runnable() { @Override public void run() { yourView.updateHoweverYouLike(newScoreResult); } })
- ...
- };
- downloadScheduler.scheduleAtFixedRate(scoreHttpRunnable, 0, 4, TimeUnit.SECONDS);
- AsyncTasks should ideally be used for short operations (a few seconds at the most.)
- @Override
- protected String doInBackground(String... params) {
- Log.d(TAG, "type - " + params[0] + ", url = " + params[1] + ", name = " + params[2]);
- downloadFile(params[1], params[2]);
- return null;
- }
- URL url = new URI(Url.replace(" ", "%20")).toURL();
- URLConnection connection = url.openConnection();
- connection.setConnectTimeout(1000);
- int fileLength = connection.getContentLength();
- mSavePath = CommonUtilities.getFileSavePath(mContext, fileName, fileLength);
- Log.d(TAG, "*** saveFilePath - " + mSavePath);
- InputStream inputStream = connection.getInputStream();
- if (inputStream != null) {
- File file = new File(mSavePath);
- BufferedOutputStream bufferOutputStream = new BufferedOutputStream(new FileOutputStream(file));
- byte byteArray[] = new byte[1024];
- int len = 0;
- long total = 0;
- while ((len = inputStream.read(byteArray)) != -1) {
- bufferOutputStream.write(byteArray, 0, len);
- total += len;
- }
- bufferOutputStream.flush();
- bufferOutputStream.close();
- inputStream.close();
- } else {
- Log.d(TAG, "*** inputStream is null");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement