Download Image via HTTP (not asynchronly)

CryoGenID

Active Member
Licensed User
Longtime User
Download Image via HTTP (synchronously)

Hello Community ;-)

I am currently stuck with the problem how to download a picture via HTTP, store that temporarily on the device and write it is a BLOB into an SQLite-Table.

I have read a lot here, but only found ways to asynchronously download s.th. via HTTP. But I need (as the downloads have to happen at a certain time and I want to wait for the download to finish) the download synchronously...

I would be very happy if you could help me and point me to the right direction!

Thanks a lot and best regards,

Chris :)
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Downloading the image synchronously will not be faster than asynchronously. It will only cause your application to show the ANR dialog (application not responding) if the download will take more than 5 seconds.

You can show a ProgressDialog and when the StreamFinish event is raised, hide the ProgressDialog and do whatever you want with the file.
 
Upvote 0

CryoGenID

Active Member
Licensed User
Longtime User
Erel,

thanks for your reply.
The reason why I would like to do the transfer synchronously is, that I download a file, open it and then go through it line by line. For each line, I have to do some things, and part of those things is the download of a picture. And I have to wait until that picture is downloaded and saved correctly until I can go to the next line in the file and work with the data from there.

So the problem is, that I cannot work asynchronously but have to do it step by step :-(

(btw. the pictures are really small, so they should download in <1 second)

I would be very happy if you could help me out with this problem :)

Thanks and best regards,

Chris
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The first part of the connection is always asynchronous. Making the connection can take several seconds depending on many parameters. The second part where you read the stream can be done synchronously by calling: Response.GetInputStream and work with the stream. You can read the stream and save it manually to a file or you can instead use GetAsynchronously and it will do the work for you.
 
Upvote 0

CryoGenID

Active Member
Licensed User
Longtime User
Erel, thanks for your reply.

I am really pulling my hair out because if this :sign0085:

Possibly I am already thinking so much about this problem, that I can't see the easiest solution...

I have now worked with downloading asynchronously, but now I run into the problem of "java.util.concurrent.RejectedExecutionException" because I now have too many connections (~ 28).

I am doing some kind of batch processing here, so downloading a JSON-File from the internet, taking out informations line by line and processing them.
For each line: First I insert stuff into a DB and then I need to download a picture and save it to the device and do stuff with the info from the current line and picture.

The problem is, I don't know how I can wait for a successful download before I continue working with the next line.
My preferred way would be:
- Download JSON-File:
1) Read the next line
2) Add stuff to the DB
3) Download the picture
4) Wait for the Download to complete
5) Work with the data of the current line of the JSON-File AND the picture
6) Goto 1)

:sign0085: :)

Thanks a lot for any help!

Best regards,

Christian
 
Upvote 0

CryoGenID

Active Member
Licensed User
Longtime User
Erel,

thanks again for your reply :)

But how can I make the "main"-thread wait until the download is complete (Step 4 in my last post)?

If I make a "do nothing until variable = true"-Statement, the whole application locks up...

Thanks a lot and best regards,

Christian
 
Upvote 0

CryoGenID

Active Member
Licensed User
Longtime User
Ok, finally solved it :sign0060:

I have now created some sort of queuing, so that I only have one download each...

Thanks again for all you help and support! :)

Best regards,

Christian
 
Upvote 0

Ratters

Member
Licensed User
Longtime User
Ok, finally solved it :sign0060:

I have now created some sort of queuing, so that I only have one download each...

Thanks again for all you help and support! :)

Best regards,

Christian
Hi Christian,

I am having the same headache :(

Any chance you can show me some sample code to see how you got round this ?

Cheers

Robbie
 
Upvote 0

CryoGenID

Active Member
Licensed User
Longtime User
Robbie,

sorry for my late response to your post :(

Here is how I solved it:
I created a (public) MAP, which holds the downloads I need to do, e.g. 10 filenames to download.
I then have a "download"-sub, which looks into the map if there are still entries, takes the first one, and starts a download. In the XX_StreamFinish - Sub, I remove the first one from the map and call the "download"-sub again.

The "Download"-Sub takes again the first entry of the map, starts the download, ...
I also check in the "Download"-Sub if the MAP is empty, if yes, then all neccessary downloads have been done.

It is as easy as that ;-)

Just reply to this post if you have any more questions, I hope that I could help you a little bit :)

Best regards,

Chris
 
Upvote 0
Top