Italian Ciao a tutti come converto questo codice da vb net a b4a

diresc

Member
Licensed User
Longtime User
Dim handler = New HttpClientHandler()
handler.UseCookies = False


Using httpClient = New HttpClient(handler)
Using request = New HttpRequestMessage(New HttpMethod("GET"), "http://192.168.1.35/FileBrowser/Download?Path=/DataLogs/DOSAGGIO.csv")
request.Headers.TryAddWithoutValidation("Referer", "http://192.168.1.35/Portal/Portal.mwsl?PriNav=FileBrowser&Path=/DataLogs")
request.Headers.TryAddWithoutValidation("Cookie", "cookie.txt")

Dim response = Await httpClient.SendAsync(request)
result = Await response.Content.ReadAsStringAsync()
If result IsNot Nothing And result.Length > 0 Then
Debug.WriteLine(result.Substring(0, 100))
End If
' TextBox2.Text = result
' DataGridView1.DataSource = result
'TextBox1.Text = result.Substring(0, 100)
End Using
End Using
'lettura
Dim tr = New MemoryStream(Encoding.UTF8.GetBytes(result))




Dim sw = New StreamReader(tr)



ProgressBar1.Value = 0
ProgressBar1.Maximum = 1001
Dim CONTA = 1000
For I = 0 To CONTA


If sw.Peek() <> -1 Then
Dim Str2() As String = Split(sw.ReadLine(), ",")
If I < 1 Then
For T = 0 To 7 '9
DataGridView1.Columns(T).HeaderText = Str2(T)

Next T


Else

'DataGridView1.Rows.Add(Str2(0), Str2(1), Str2(2), Str2(3), Str2(4), Str2(5), Str2(6), Str2(7), Str2(8), Str2(9)) '
DataGridView1.Rows.Add(Str2(0), Str2(1), Str2(2), Str2(3), Str2(4), Str2(5), Str2(6), Str2(7))
End If

Else
End If


ProgressBar1.Value += 1
Next I
sw.Close()
'FILE SALVA OK
Dim path As String = My.Application.Info.DirectoryPath
Dim memString As String = result
' convert string to stream
Dim buffer As Byte() = Encoding.ASCII.GetBytes(memString)
Dim ms As New MemoryStream(buffer)
'write to file
Dim file As New FileStream(path & "\DOSAGE.csv", FileMode.Create, FileAccess.Write)
ms.WriteTo(file)
file.Close()
ms.Close()
 

LucaMs

Expert
Licensed User
Longtime User
Anzi, provo io, dai...
Ecco cosa ha risposto ChatGPT (sarà sicuramente pieno di errori ma una buona base):

B4X:
Dim job As HttpJob
job.Initialize("", Me)
job.Download("http://192.168.1.35/FileBrowser/Download?Path=/DataLogs/DOSAGGIO.csv")

ProgressDialogShow("Downloading...")

Wait For (job) JobDone(job As HttpJob)
ProgressDialogHide

If job.Success Then
    Dim result As String = job.GetString
    Log(result.SubString2(0, 100))
    
    Dim tr As InputStream
    tr.InitializeFromBytesArray(result.GetBytes("UTF8"))
    
    Dim sw As TextReader
    sw.Initialize(tr)
    
    ProgressBar1.Value = 0
    ProgressBar1.Maximum = 1001
    Dim CONTA As Int = 1000
    For I = 0 To CONTA
        If sw.Peek <> -1 Then
            Dim Str2() As String = Regex.Split(",", sw.ReadLine)
            If I < 1 Then
                For T = 0 To 7 '9
                    DataGridView1.Columns.Get(T).HeaderText = Str2(T)
                Next
            Else
                'DataGridView1.Rows.Add(Str2(0), Str2(1), Str2(2), Str2(3), Str2(4), Str2(5), Str2(6), Str2(7), Str2(8), Str2(9)) '
                DataGridView1.AddSingleLine(Str2(0) & "," & Str2(1) & "," & Str2(2) & "," & Str2(3) & "," & Str2(4) & "," & Str2(5) & "," & Str2(6) & "," & Str2(7))
            End If
        End If
        ProgressBar1.Value = ProgressBar1.Value + 1
    Next
    sw.Close
    
    ' FILE SALVA OK
    Dim path As String = File.DirInternal
    Dim memString As String = result
    Dim buffer() As Byte = memString.GetBytes("ASCII")
    Dim ms As OutputStream
    ms.InitializeToBytesArray(buffer)
    Dim file As OutputStream
    file = File.OpenOutput(path, "DOSAGE.csv", False)
    file.WriteBytes(buffer, 0, buffer.Length)
    file.Close
    ms.Close
End If
job.Release
 

Sagenut

Expert
Licensed User
Longtime User
Proviamo in un altro modo.
Pare che devi scaricare un certo file CSV.
Poi come vuoi mostrare questi dati?
Quale risultato vuoi ottenere?
Il file è di libero accesso?
 

LucaMs

Expert
Licensed User
Longtime User
Per il download, con ciò che hai fornito, dovrebbe essere così:
B4X:
Sub DownloadFile
    Dim Job As HttpJob
    Job.Initialize("JobName", Me)
    Job.Download("http://192.168.1.35/FileBrowser/Download?Path=/DataLogs/DOSAGGIO.csv")
    Job.GetRequest.SetHeader("Referer", "http://192.168.1.35/Portal/Portal.mwsl?PriNav=FileBrowser&Path=/DataLogs")
    Job.GetRequest.SetHeader("Cookie", "cookie.txt")

    Wait For (Job) JobDone(Job As HttpJob)
    If Job.Success Then
        Log(Job.GetString)
    Else
        Log("Errore durante il download del file: " & Job.ErrorMessage)
    End If
    Job.Release
End Sub
ma mi dà questo errore:
ResponseError. Reason: java.net.ConnectException: Failed to connect to /192.168.1.35:80

Sei sicuro di quegli url? E, soprattutto, ti funziona con VB Net?
 
Last edited:

diresc

Member
Licensed User
Longtime User
una domanda
1)come faccio ad incollare il codice come hai fatto tu es. B4X: codice
il codice funziona sia in net che B4A il problema di rifiuto è http// non https//
ho aggiunto in manifest :SetApplicationAttribute(android:usesCleartextTraffic, "true")'aggiunto per https cosi funziona
se mi spieghi ti invio il codice che forse andrà ripulito ma comunque vedo i miei dati del csv con 4 righe righe di codice
Grazie mille per il momento
 

Sagenut

Expert
Licensed User
Longtime User
Per postare del codice usa l'icona
</>
e si aprirà la finestra dove mettere il codice.
 

diresc

Member
Licensed User
Longtime User
sia in net e con b4a
Screenshot 2024-02-20 225648.png
Screenshot 2024-02-20 225340.png
 

diresc

Member
Licensed User
Longtime User
Test da lavorare:
Sub Button1_Click
    'xui.MsgboxAsync("Hello world!", "B4X")
    
    B4XTable1.MaximumRowsPerPage = 10
    B4XTable1.BuildLayoutsCache(10)
    

    Dim datax1 As List
    datax1.Initialize
    

    job4.Initialize("Job4", Me)
    job4.Download("http://192.168.1.35/FileBrowser/Download?Path=/DataLogs/DOSAGGIO.csv") 'it can also be PostString or any of the other methods
    job4.GetRequest.SetHeader("Referer", "http://192.168.1.35/Portal/Portal.mwsl?PriNav=FileBrowser&Path=%2FDataLogs")
    job4.GetRequest.SetHeader("Cookie", "cookie.txt")
    
End Sub
Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    B4XTable1.Clear
        Dim datax As List
    datax.Initialize
    If Job.Success = True Then
        
        
        Select Job.JobName
            Case "Job1", "Job2"
                'VEDRO DOPO
            Case "Job3"
                'VEDRO DOPO
                'ImageView1.SetImage(Job.GetBitmap)
            Case "Job4"
            '    QUELLO CHE MI SERVE PER IL MOMENTO
                Dim result As String = Job.GetString
                Dim Rows() As String = Regex.Split(CRLF, result)
                
                ' Aggiungiamo le intestazioni alla griglia
                Dim Headers() As String = Regex.Split(",", Rows(0))
                
            
                Dim I As Int
                For I = 0 To Headers.Length - 1
                
                    B4XTable1.AddColumn(Headers(I), B4XTable1.COLUMN_TYPE_TEXT)
            
                
                Next
        
                ' Aggiungiamo le righe alla griglia
                For I = 1 To Rows.Length - 1
                Dim Values() As String = Regex.Split(",", Rows(I))
                    datax.Add(Array(Values(0), Values(1), Values(2),Values(3), Values(4), Values(5), Values(6), Values(7)))
                B4XTable1.SetData(datax)
            
            
            
                Next
                
                
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
    End If
    Job.Release
End Sub
 

Sagenut

Expert
Licensed User
Longtime User
Bene se sei riuscito a fare funzionare la cosa. :)
 

Sagenut

Expert
Licensed User
Longtime User
Il codice lo ha fornito @LucaMs
 
Top