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

Discussion in 'Android Questions' started by sdesan, Jul 24, 2015.

  1. sdesan

    sdesan Member Licensed 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
    Code:
    Sub Globals
        
    Private MyButton As Button
        
    Private link1 As String
        
    Dim job1 As HttpJob
    End Sub
    Code:
    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
     
  2. Enrique Gonzalez R

    Enrique Gonzalez R Well-Known Member Licensed User

    Code:
    job1.GetInputStream(fileurl)
    why are you passing the fileurl? getinpustream does not need any parameter.

    and for the 10 single one by one:

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

    Roycefer Well-Known Member Licensed User

    You can't just call job1.Success and job1.GetInputStream immediately after calling job1.Download. You have to wait for the JobDone event to fire and then you can deal with its InputStream.
     
  4. sdesan

    sdesan Member Licensed 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 :) )
     
    Enrique Gonzalez R likes this.
  5. Enrique Gonzalez R

    Enrique Gonzalez R Well-Known Member Licensed 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: http://www.b4x.com/android/forum/th...ges-with-httputils2-and-customlistview.26078/

    code:

    Code:
    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
     
  6. sdesan

    sdesan Member Licensed User

    Finally i used this solution:

    Code:
    Sub Process_Globals
        
    Dim httpC As HttpClient
    End Sub
    Code:
    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
    Code:
    Sub Activity_Create(FirstTime As Boolean)
        
    If FirstTime Then
           httpC.Initialize(
    "httpC")
        
    End If
        
    Activity.LoadLayout("mylayout")
    End Sub
    Code:
    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
    Code:
    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
    Code:
    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
    Code:
    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
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Your solution is not good.

    You should instead use HttpUtils2 and handle JobDone event. It is quite simple.
     
    Last edited: Jul 27, 2015
    lemonisdead likes this.
  8. Erel

    Erel Administrator Staff Member Licensed User

    Here is the correct code:
    Code:
    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
     
    Enrique Gonzalez R likes this.
  9. sdesan

    sdesan Member Licensed User

    Thank you Erel
    ASAP i'll made modifcations.
     
Loading...