Android Question OkHttpUtils2

Angelo Messina

Active Member
Licensed User
Longtime User
Hi everyone, I'm implementing an app that needs to update a table in a SQLite3 database, and when using okhttputils, the added records are always duplicated. Can you explain the reason? I've attached the code to insert the data.

B4X:
Sub UpDateData(Sito As String,n_anno As String, n_mese As String, n_giorno As String, n_kwh As String)

    Dim a As Int = 0   
    If (Vecchio>0 And Vecchio<=Cosa) Then
    
        Dim job1 As HttpJob
        Dim requestBody As String
        Dim Id1 As Int = Vecchio
        job1.Initialize("job1", Me)
        Dim kwh As Float = n_kwh
        ' Costruzione del corpo della richiesta
        Dim requestBody As String = $"Id=${Vecchio}&anno=${n_anno}&mese=${n_mese}&giorno=${n_giorno}&kwh=${n_kwh}"$
        Log("PUT requestBody: " & requestBody)
        job1.PostString(Sito,"PUT")
        job1.PutString(Sito, requestBody)
        job1.GetRequest.SetContentType("text/plain")
        
        Wait For (job1) JobDone(job1 As HttpJob)
        If job1.Success Then
            Log("✅ Risposta job1: " & job1.GetString)
        Else
            Log("❌ Errore job1: " & job1.ErrorMessage)
        End If
        job1.Release
    Else
    
        Dim job5 As HttpJob
        Dim requestBody As String
        job5.Initialize("job5", Me)
        Dim kwh As Float = n_kwh
        '"ClientPost",Array(url,aa,mm,gg,Kw))
        '   POST Richiesta anno=2025&mese=08&giorno=29&kwh=2.24
        requestBody = $"anno=${n_anno}&mese=${n_mese}&giorno=${n_giorno}&kwh=${kwh}"$
        Log("POST Richiesta " & requestBody)
        job5.PostString(Sito,"POST")
        ' Costruzione URL completo (se il server accetta parametri nella URL)
        Dim fullUrl As String = Sito & "/" & requestBody
        ' Invio POST con corpo identico ai parametri
        Dim requestBytes1() As Byte = requestBody.GetBytes("UTF8")
        job5.PostBytes(fullUrl, requestBytes1)
        job5.GetRequest.SetContentType("text/plain")
        
        Wait For (job5) JobDone(job5 As HttpJob)
        If job5.Success Then
            Log("✅ Risposta job5: " & job5.GetString)
            Return
        Else
            Log("❌ Errore job5: " & job5.ErrorMessage)
        End If
        job5.Release
        Return
    End If
    
End Sub
 

Angelo Messina

Active Member
Licensed User
Longtime User
the same App written with B4J I used java to update the server
B4X:
public static int ClientPost(String Sito,String nanno,String nmese,String ngiorno,String nkwh) throws IOException
{
    try
     {
      // Dati da inviare al server
        String anno   = nanno;
        String mese   = nmese;
        String giorno = ngiorno;
        float kwh     = Float.parseFloat(nkwh);
        String requestBody = "anno=" + anno + "&mese=" + mese + "&giorno=" + giorno + "&kwh=" + kwh;
        // Creazione dell'URL del server
        URL url = new URL(Sito+"/"+requestBody);
        // Creazione della connessione HTTP
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "text/plain");
         System.out.println("POST Richiesta " + requestBody);

        // Invio del corpo della richiesta
        try (OutputStream os = conn.getOutputStream())
        {
               byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
               os.write(input);
            os.flush();
        }
        // Controllo della risposta del server
        int responseCode = conn.getResponseCode();
        // System.out.println("Response Code: " + responseCode);
        // Chiusura della connessione
        conn.disconnect();
        return responseCode;
    }
    catch (Exception e)
    {
        System.out.println("Errore del Server");
        return 500;
    }
}

public static int ClientPut(String Sito,int ID,String nanno,String nmese,String ngiorno,String nkwh) throws IOException
{
    try
      {
          // Dati da inviare al server
          int id1    = ID;
        String anno   = nanno;
        String mese   = nmese;
        String giorno = ngiorno;
        float kwh     = Float.parseFloat(nkwh);
        String requestBody = "id=" + id1 + "&anno=" + anno + "&mese=" + mese + "&giorno=" + giorno + "&kwh=" + kwh;
        // Creazione dell'URL del server
        URL url = new URL(Sito+"/"+requestBody);
        // Creazione della connessione HTTP
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("PUT");
        conn.setDoOutput(true);
        conn.setRequestProperty("Content-Type", "text/plain");
         // System.out.println("PUT Richiesta " + requestBody);
        // Invio del corpo della richiesta
        try (OutputStream os = conn.getOutputStream())
        {
               byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
               os.write(input);
            os.flush();
        }
        // Controllo della risposta del server
        int responseCode = conn.getResponseCode();
        // System.out.println("Response Code: " + responseCode);
        // Chiusura della connessione
        conn.disconnect();
        return responseCode;
    }
    catch (Exception e)
    {
        e.printStackTrace();
        return 500;
    }
}

and it works great
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Your code with PostString and PutString is wrong.
Please post your question in Question forum.
 
Upvote 0
Top