Carthalion Member Licensed User Longtime User Sep 30, 2016 #1 Please do not chastise me for not trying to research this first. I have spent most of the day reading examples for HTTP, HttpUtils2, OKHTTP & OKHttpUtils2. One shows how to populate a string array. Another, a huge image file but I cannot get it to work. I think that what I am trying to do is fairly simple. I want to download and save a text file from a server. This code seems to work: Dim job As HttpJob job.Initialize("Job", Me) job.Download("http://www.MyServer.com/textfiles/MyFile.txt") The Sub JobDone gives me a success message. But where is the file? I don't know. Can I get a code sample that finds the server and saves the file to my project? Thanks, Carthalion
Please do not chastise me for not trying to research this first. I have spent most of the day reading examples for HTTP, HttpUtils2, OKHTTP & OKHttpUtils2. One shows how to populate a string array. Another, a huge image file but I cannot get it to work. I think that what I am trying to do is fairly simple. I want to download and save a text file from a server. This code seems to work: Dim job As HttpJob job.Initialize("Job", Me) job.Download("http://www.MyServer.com/textfiles/MyFile.txt") The Sub JobDone gives me a success message. But where is the file? I don't know. Can I get a code sample that finds the server and saves the file to my project? Thanks, Carthalion
lemonisdead Well-Known Member Licensed User Longtime User Sep 30, 2016 #2 Hello, This may help (second code sample of first post) : https://www.b4x.com/android/forum/threads/download-image-or-file-from-a-website.39412/ The result is saved using an OutputStream : https://www.b4x.com/android/help/files.html#outputstream Upvote 0
Hello, This may help (second code sample of first post) : https://www.b4x.com/android/forum/threads/download-image-or-file-from-a-website.39412/ The result is saved using an OutputStream : https://www.b4x.com/android/help/files.html#outputstream
Carthalion Member Licensed User Longtime User Sep 30, 2016 #3 lemonisdead said: Hello, This may help (second code sample of first post) : https://www.b4x.com/android/forum/threads/download-image-or-file-from-a-website.39412/ The result is saved using an OutputStream : https://www.b4x.com/android/help/files.html#outputstream Click to expand... Hi lemonisdead and thank you for your input. These are both samples that I have tried to make work. I get an error. (Screen shot attached.) If you can look at my code and tell me what you see, I would appreciate it. Also, I can't call the JobDone Sub. It wants a missing parameter and I cannot determine what it would be. Dim job As HttpJob job.Initialize("j", Me) job.Download("http://www.MyServer.com/textfiles/MyFile.txt") Sub JobDone(job As HttpJob) If job.Success Then Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "filename.dat", False) File.Copy2(job.GetInputStream, out) out.Close '<------ very important Else Log("Error: " & job.ErrorMessage) End If job.Release End Sub Thank you, Carthalion Attachments JobDone error002.jpg 68.8 KB · Views: 661 Upvote 0
lemonisdead said: Hello, This may help (second code sample of first post) : https://www.b4x.com/android/forum/threads/download-image-or-file-from-a-website.39412/ The result is saved using an OutputStream : https://www.b4x.com/android/help/files.html#outputstream Click to expand... Hi lemonisdead and thank you for your input. These are both samples that I have tried to make work. I get an error. (Screen shot attached.) If you can look at my code and tell me what you see, I would appreciate it. Also, I can't call the JobDone Sub. It wants a missing parameter and I cannot determine what it would be. Dim job As HttpJob job.Initialize("j", Me) job.Download("http://www.MyServer.com/textfiles/MyFile.txt") Sub JobDone(job As HttpJob) If job.Success Then Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "filename.dat", False) File.Copy2(job.GetInputStream, out) out.Close '<------ very important Else Log("Error: " & job.ErrorMessage) End If job.Release End Sub Thank you, Carthalion
DonManfred Expert Licensed User Longtime User Sep 30, 2016 #4 you should copy the TEXT from the logwindow instead of posting a screenshot (where the inportant info is hidden) Last edited: Sep 30, 2016 Upvote 0
you should copy the TEXT from the logwindow instead of posting a screenshot (where the inportant info is hidden)
DonManfred Expert Licensed User Longtime User Sep 30, 2016 #5 Carthalion said: Also, I can't call the JobDone Sub. Click to expand... you should NOT call this sub directly! It is raised from a running httpjob after the job finishes (with success or not) Upvote 0
Carthalion said: Also, I can't call the JobDone Sub. Click to expand... you should NOT call this sub directly! It is raised from a running httpjob after the job finishes (with success or not)
fixit30 Active Member Licensed User Longtime User Sep 30, 2016 #6 The attached sample project works fine for me? Attachments DownloadText.zip DownloadText.zip 7.1 KB · Views: 655 Upvote 0
Carthalion Member Licensed User Longtime User Sep 30, 2016 #7 fixit30 said: The attached sample project works fine for me? Click to expand... Actually, the code works for me also when I put it (and it only) in a new project. So there must be something else causing the error. I do still have a question that I hope you can help me with. When I run the code, I get the success message that the file was downloaded. But where is it? I would think that it would show up in my files folder but it is not there. When you ran the code, did you get a text file (InetLoLo.text)? Thank you for your help! Upvote 0
fixit30 said: The attached sample project works fine for me? Click to expand... Actually, the code works for me also when I put it (and it only) in a new project. So there must be something else causing the error. I do still have a question that I hope you can help me with. When I run the code, I get the success message that the file was downloaded. But where is it? I would think that it would show up in my files folder but it is not there. When you ran the code, did you get a text file (InetLoLo.text)? Thank you for your help!
fixit30 Active Member Licensed User Longtime User Sep 30, 2016 #8 Carthalion said: When you ran the code, did you get a text file Click to expand... Yes. I got the file. My files, device storage. (Samsung) The root of your devices internal storage. Upvote 0
Carthalion said: When you ran the code, did you get a text file Click to expand... Yes. I got the file. My files, device storage. (Samsung) The root of your devices internal storage.
Carthalion Member Licensed User Longtime User Oct 1, 2016 #9 You are right! I was just not looking for it in the right place. Thank you. Upvote 0