android


Download files from server freezes app and loses connection


I'm triying to download some files from a FTP Server with FTPClient (commons-net-3.3).
When click in a button with a AsyncTask I get a list of FTPFile s with listFiles(path);. Then I download each file of list like this (All of work in the same AsyncTask)
FileOutputStream desFileStream = new FileOutputStream("localfile");
InputStream input = mFTPClient.retrieveFileStream("remotepath");
byte data[] = new byte[512];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
Double porcentaje = ((double) total / (double) tmpFiled.getSize()) * 100;
publishProgress((int) Math.round(porcentaje));
desFileStream.write(data, 0, count);
}
desFileStream.flush();
desFileStream.close();
input.close();
}}
In particular in input.read(data). This must be read 1024 bytes but in random iteration read less data than 1024, write it and in the next iteration input.read(data) freezes download return timedout exception
After that the internet connection of the dispositive not works
Logcat shows this exception
09-19 09:59:06.890: W/System.err(13215): java.net.SocketException: recvfrom failed: ETIMEDOUT (Connection timed out)
09-19 09:59:06.900: W/System.err(13215): at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:545)
09-19 09:59:06.900: W/System.err(13215): at libcore.io.IoBridge.recvfrom(IoBridge.java:509)
09-19 09:59:06.900: W/System.err(13215): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
09-19 09:59:06.920: W/System.err(13215): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
09-19 09:59:06.920: W/System.err(13215): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
09-19 09:59:06.920: W/System.err(13215): at java.io.FilterInputStream.read(FilterInputStream.java:118)
09-19 09:59:06.920: W/System.err(13215): at java.io.InputStream.read(InputStream.java:162)
09-19 09:59:06.920: W/System.err(13215): at com.asde.telemedicina.FTPHandler.ftpDownload(FTPHandler.java:405)
09-19 09:59:06.920: W/System.err(13215): at com.asde.telemedicina.FTPHandler.doInBackground(FTPHandler.java:167)
09-19 09:59:06.920: W/System.err(13215): at com.asde.telemedicina.FTPHandler.doInBackground(FTPHandler.java:1)
09-19 09:59:06.920: W/System.err(13215): at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-19 09:59:06.920: W/System.err(13215): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-19 09:59:06.920: W/System.err(13215): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-19 09:59:06.930: W/System.err(13215): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-19 09:59:06.930: W/System.err(13215): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-19 09:59:06.930: W/System.err(13215): at java.lang.Thread.run(Thread.java:841)
09-19 09:59:06.930: W/System.err(13215): Caused by: libcore.io.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out)
09-19 09:59:06.930: W/System.err(13215): at libcore.io.Posix.recvfromBytes(Native Method)
09-19 09:59:06.930: W/System.err(13215): at libcore.io.Posix.recvfrom(Posix.java:141)
09-19 09:59:06.930: W/System.err(13215): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
09-19 09:59:06.930: W/System.err(13215): at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
09-19 09:59:06.930: W/System.err(13215): ... 14 more
For example in Motorola Moto G never occurs however in other dispositives frecuently occurs. I',e tried two ftpservers.
Can cause the problems local file permissions?
Can anyone help me out?
I think that your error could be caused by multiple active downloads connection which saturate your network connection. And generate a timeout exception. It should explain the random characteristic of you error. To avoid it I think you have to limitate the number of simultaneous download.
After doing a little search on google I've also carried out (even if i think that isn't your case) that the "Connection Timeout" error In Ice Cream Sandwich and earlier could be related to DNS double caching. See this Issue
I've done little search and try that;
FileOutputStream desFileStream = new FileOutputStream("localfile");
mFTPClient.setFileType(FTP.BINARY_FILE_TYPE); // File type can be changed
boolean status = mFTPClient.retrieveFile("remotepath",desFileStream);
In particular in input.read(data). This must be read 1024 bytes but in random iteration read less data than 1024
No it's not "must". Here is what Java doc sais:
int read(byte[] b)
Reads some number of bytes from the input stream and stores them into the buffer array b.
This function reads data up to buffer size, not exactly that size.
InputStream input = mFTPClient.retrieveFileStream("remotepath");
What is "remotepath"?
Is it a file, or is it a directory?
In case it's a file, problem is in the line input.close();, because after you've read few bytes on the first iteration you close your stream and then trying to read from the closed stream on the second iteration (like already been said in comment).
In case it's a directory, then you just can't download it like you're trying to. There's a LIST command in FTP protocol, which lists files in a current directory. You must list those files and download every of them separately.

Related Links

android app update and resuming activity
Animate Tabs in TabLayout
How to get URL of cloud-nary video in android
File does not get deleted - RecyclerView
Add item purchases to android app from African country
Reliable close Android Keyboard
GPS time not stopping
Z-order changed in activity transition animations on android 7.0 (nougat)?
Why does Android Emulator disconnect after changing cellular network status?
Cannot build libtess.so in tess-two
How to make a tabview which is compatible for android 4.4 to 6
Seekbar background in fragments is invisible after loading webview in another fragment
Visual Studio Emulator for Android option is not exists
Android 4.0 cart badge NullPointerException
Properly unlock android device kitkat programatically
android collapses so we restart everyday

Categories

HOME
telegram
nunit
rsa
battery
cq5
spring-session
python-multithreading
android-json
iis-6
recordrtc
appcelerator-studio
feathersjs
digital-signature
identity-management
shortest-path
jmp
connector
lilypond
imageresizer
expressionengine3
jquery-multiselect
fragment-backstack
winexe
failover
sqlpackage
magento-1.8
mobx
livelink
zend-debugger
cart
joomla2.5
hunspell
baidu-map
servicestack-text
sharepoint-workflow
lint
git-rewrite-history
cd
release
odroid
verifiable-c
datacachefactory
chronicle-map
congestion-control
importerror
insight
azure-cli
mach-o
reachability
rocks
simplepie
burrows-wheeler-transform
nservicebus5
redis-cluster
design-principles
crouton-os
jnlua
grgit
graphics2d
diff3
document-repository
execve
genymotion-call
human-computer-interface
ios8-extension
menubar
gae-quotas
openargs
tlf
joomla-template
font-awesome-4.0.0
android-icons
robotlegs
veracity
jdb
user-tracking
workflow-services
u2netdk
automationelement
tournament
drawtobitmap
django-paypal
appledoc
load-time
message-passing
pagemethods
lxr
build-environment
cldc
dojo-dnd
unmanagedresources

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App