Help: Getting a file from internet and store in variable

Put your problem here if it does not fit any of the other categories.

Help: Getting a file from internet and store in variable

Postby marvin0815 » Mon Jan 10, 2011 11:51 am

Hello dear board users,

i'm looking for a way to open a destination file in the internet into a variable for my android app:

for example:

"www.example.com/de/index/date.txt" = URL

then i want to store the data which is in the date.txt in a variable in the APP for further processing. can someone show me some sourcecode doing this? i wish someone can help me.

regards
Marvin Dulowan
marvin0815
Freshman
Freshman
 
Posts: 2
Joined: Mon Jan 10, 2011 11:44 am

Top

Re: Help: Getting a file from internet and store in variable

Postby jonbonazza » Mon Jan 10, 2011 3:24 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. byte[] downloadFile(String urlString)
  2. {
  3.         URL url = new URL(urlString);
  4.         byte[] bytes;
  5.         URLConnection conn = url.openConnection();
  6.         InputStream in = conn.getInputStream();
  7.         BufferedInputStream bis = new BufferedInputStream(in);
  8.         ByteArrayBuffer baf = new ByteArrayBuffer(50);
  9.         int current = 0;
  10.         while((current = bis.read()) != -1)
  11.         {
  12.               baf.append((byte)current);
  13.         }
  14.        
  15.         bytes = baf.toByteArray();
  16.         in.close()
  17.         bis.close();
  18.        
  19.         return bytes;
  20. }
  21.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Then you would use it like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. byte[] fileData = downloadFile(fileUrl);
  2. //perform processing at the binary level
  3.  
Parsed in 0.029 seconds, using GeSHi 1.0.8.4
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Re: Help: Getting a file from internet and store in variable

Postby marvin0815 » Tue Jan 11, 2011 7:23 am

Thank you many times.

Is there any site with basic examples of such functionality. Im coming from C++ using QT SDK.
marvin0815
Freshman
Freshman
 
Posts: 2
Joined: Mon Jan 10, 2011 11:44 am

Re: Help: Getting a file from internet and store in variable

Postby jonbonazza » Wed Jan 19, 2011 8:22 pm

Hmm.. You would have to search. I do not know of any off the top of my head.
jonbonazza
Master Developer
Master Developer
 
Posts: 665
Joined: Thu Jul 15, 2010 2:58 pm
Location: WV USA

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 17 guests