java


URLConnection time out issue


I am working in an android application and I am downloading a file from an url. Every thing works fine, but when the internet connection goes in between(After opening a connection) the downloading time out never occurs and the connection never ends.
Suggest me a solution to solve this issue
**URL url = new URL("fileURL");
URLConnection connection = url.openConnection();
connection.setConnectTimeout(5000);
File file = new File(context.getFilesDir(), "" + filename);
// getting file length
int lenghtOfFile = connection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
int status = (int) ((total * 100) / lenghtOfFile);
publishProgress("" + status);
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close()**
You can use Retrofit Library to download files from server,
Retrofit uses OkHttp internally
Please refer below URL,
https://futurestud.io/tutorials/retrofit-2-how-to-download-files-from-server
final FileDownloadService downloadService =
ServiceGenerator.createService(FileDownloadService.class);
Call<ResponseBody> call =
downloadService.downloadFileWithDynamicUrlSync(fileUrl);
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, final Response<ResponseBody>
response) {
if (response.isSuccessful()) {
Log.d(TAG, "server contacted and has file");
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... voids) {
boolean writtenToDisk = writeResponseBodyToDisk(FileDownloadActivity.this, response.body(), null);
Log.d(TAG, "file download was a success? " + writtenToDisk);
return null;
}
}.execute();
} else {
Log.d(TAG, "server contact failed");
}
}
And you can also use #Streaming annotation for large files. Retrofit will handle the large file download also

Related Links

Infinite while loop with an iterator, hasNext() and next()
Cannot produce top level JAXBs from wsdl file using xjc compiler and binding file
Chess engine. Is it ok to pass board object reference to a piece?
MD5 hashing different values
How do I update the code of my java game with github
Handler.postDelayed v/s Runnable.run. Is it alright to call .run instead of .postDelayed?
How to auto stretch detail band when print order is set to “horizontal” in JasperReport?
JAX-WS and JAX-RS service class instantiation
Java - Adding another String value to existing HashMap Key without overwriting?
Taking Backup of MySQL database using Java [closed]
Create multiple Array lists by using Hashmap
Regex search in a file
String conversion of uint8_t not being recognized in java
fetching meetings using EWS
Cannot connect to database using hibernate with Microsoft Access
Displaying the possible paths of nodes from textfile into jtextfield java swing

Categories

HOME
ms-access
proxy
mediawiki
kde
session
tesseract
rubygems
baqend
yum
wamp
autotools
google-cloud-ml
spring-tool-suite
phaser
foselasticabundle
seaborn
apache-metamodel
text-rendering
jprofiler
autocad-plugin
errorlevel
bootstrap-material-design
rst2pdf
maquette
ejabberd-module
c++-amp
google-sites-2016
asset-pipeline
android-kernel
ghost4j
catalog
ios5
plsql-psp
hockeyapp
libvpx
android-browser
vapor
android-fingerprint-api
greenrobot-eventbus
wptoolkit
galleria
network-flow
knockout-components
composite-key
knpmenubundle
setuptools
static-ip-address
deadbolt-2
removechild
log4c
impresspages
websitepanel
ubuntu-10.04
master-slave
make-install
packagist
whois
ford-fulkerson
ibaction
ios9.1
interrupted-exception
composite
streambase
jsapi
clicktag
websocket4net
bitcoinj
meteor-velocity
ivyde
eventlistener
kyotocabinet
jquery-knob
google-email-migration
nsmanagedobject
asyncfileupload
gnustep
appender
mysql-error-1005
remember-me
subviews
coda-slider
suppress
firefox4
brewmp
libs
eqatec
noscript
ubuntu-9.04
lzh

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile