Android Question How to copy various txt file from a server web to DirInternal

sdesan

Member
Licensed User
Longtime User
I have made 'tons' of research and, although I made several apps that use web services to retrieve data from a web server, i'm unable to copy a simple text file from web server to my app, maybe in DirInternal folder.
I am becoming stupid?!?
This is what i tried to do
S
B4X:
Sub Globals
    Private MyButton As Button
    Private link1 As String
    Dim job1 As HttpJob
End Sub

B4X:
Sub MyButton _Click
    Dim fileurl As String
    fileurl ="http://myserver/abc.txt"
    job1.Initialize("Job1",Me)
    job1.Download(fileurl)
    If job1.Success=True Then
       File.Copy2(job1.GetInputStream(fileurl), File.OpenOutput(File.Dirinternal,"downloaded.txt",False))
    End If
End Sub

This (is the best i found in my research) don't work for an error in file.Copy2 (array expected)
My brain is 'down'.
Can anyone help me to solve this (i think very simple) problem.
(I also would like to copy 10 single file one by one)
Thank you
Sergio
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
B4X:
job1.GetInputStream(fileurl)

why are you passing the fileurl? getinpustream does not need any parameter.

and for the 10 single one by one:

B4X:
dim i = 0
dim filestodown as list
do while i < 10
dim s as string
s = filestodown.get(i)
.... (your code here)
loop
 
Upvote 0

sdesan

Member
Licensed User
Longtime User
Thank you for the answer... but i still don't have a good code for resolve my problem, a code for downloding. Enrique can you be more clear in the code (and patient :) )
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
of course!

uff man.. i can only say that Erel is way beyond of our simple minds...
i take the sample code from here: https://www.b4x.com/android/forum/t...ges-with-httputils2-and-customlistview.26078/

code:

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
Dim filenames As List
Dim jobjobs As Map

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.


End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")

filenames.Initialize
jobjobs.Initialize
filenames.AddAll(Array As String("abc","def","ghi","jkl","mno","pqr","stu","vwx","z12","345")

    For i = 0 To filenames.Size - 1
    Dim job1 As HttpJob
    Dim fileurl As String
       fileurl ="http://myserver/"& filenames.Get(i) & ".txt"
       job1.Initialize("Job1",Me)
       job1.Download(fileurl)
        jobjobs.Put(job1,filenames.Get(i))
    Next
  
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub JobDone(Job As HttpJob)
    If Job.Success Then
        Dim value As String = jobjobs.Get(Job)
            File.Copy2(Job.GetInputStream, File.OpenOutput(File.Dirinternal,value & ".txt",False))
    End If
End Sub

i cannot test it so, hope it helps
 
Upvote 0

sdesan

Member
Licensed User
Longtime User
Finally i used this solution:

B4X:
Sub Process_Globals
    Dim httpC As HttpClient
End Sub

B4X:
Sub Globals
    Private link1 As String
    Dim httpReq As HttpRequest
    Dim req,req1 As HttpRequest
    Private ii As Int
    Private MyButton As Button
End Sub

B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
       httpC.Initialize("httpC")
    End If
    Activity.LoadLayout("mylayout")
End Sub

B4X:
Sub  MyButton_Click
    ProgressDialogShow("Please wait..")
        For ii=1 To 7
            httpC.Initialize("httpC")  'Repeated for flushing buffer
            link1="http://myserver/" & i & ".txt"
            req.InitializeGet(link1)
            httpC.Execute(req, i)
            Sleep(1000) 'Wait cicle for flushing buffer
        Next
        Msgbox ("Data recovered", "Ok")
End Sub

B4X:
Sub httpC_ResponseSuccess (Response As HttpResponse, TaskId As Int)
  Dim result As String
  result = Response.GetString("UTF8")
   File.WriteString(File.DirInternal, TaskId & ".txt",result)
   ProgressDialogHide
End Sub

B4X:
Sub httpC_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
  Msgbox("Server error connection"&CRLF&CRLF&Reason,"Error ("&StatusCode&")")
  ProgressDialogHide
End Sub

B4X:
Sub Sleep(ms As Long)
Dim now As Long
  If ms > 1000 Then ms =1000  'avoid application not responding error
  now=DateTime.Now
  Do Until (DateTime.Now>now+ms)
  DoEvents
  Loop
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here is the correct code:
B4X:
Sub MyButton_Click
   Dim job1 As HttpJob
  job1.Initialize("Job1",Me)
  job1.Download("http://myserver/abc.txt")
End Sub

Sub JobDone(job As HttpJob)
   If job.Success Then
     Dim out As OutputStream = File.OpenOutput(File.Dirinternal,"downloaded.txt", False)
     File.Copy2(job.GetInputStream,  out)
     out.Close
   End If
   job.Release
End Sub
 
Upvote 0
Top