Android Question Problems with a mysql query and OkHttpUtil

Daniel44

Active Member
Licensed User
I'm working with OkHttpUtils2 and JSON on a mysql project. The application connects well to the XAMPP server. I'm working with Xampp and php 7 and mysqli. The main problem is that even though it inserts data to a table through a php file it can't read a "SELECT SUM" I mean... I need to fill a label and give the sum of a whole column but I still can not bring the sum. the query in the php file works but in B4A I still can not bring that value inside the label. Maybe it's because I still don't understand JobDone well. I've done it in a thousand ways. I've watched the Erel video where he teaches how to bring data from a Json and I've adapted it to my project and it doesn't work. If someone knows the way and helps me I thank them in advance. Thank you

This is my mysql query into operaciones.php

PHP:
case "sumarcash":
                    $sql = "SELECT SUM(monto) AS TotalesC FROM gastoscash";
                    $resultado = $mysqli->query($sql);

                    $fila=$resultado->fetch_assoc();

                    $TotalCash=$fila['TotalesC'];
                      print json_encode($TotalCash);
                    break;

there are many "case" but I can't get this one going.

what i have in Activity Module
B4X:
Public Sub Initialize (labelTemp As Label)

    labelLocal=labelTemp

End Sub

Public Sub downloadInfo  '< I call this one on Activity Create
    Log("class:downloadinfo")
    ProgressDialogShow("Obteniendo Suma..")
    remoteQuery("SELECT SUM(monto) AS TotalesF FROM gastoscash", "sumcash")
End Sub

Private Sub remoteQuery(query As String, nombreJob As String)
    Log("class:remoteQuery")
    Dim job As HttpJob
    job.Initialize(nombreJob,Me )
    job.PostString("http://localhost:8080/gastosxampp/operaciones.php", query)
End Sub

Sub JobDone(Job As HttpJob)
    Log("class:JobDone("&Job.GetString&")")
    ProgressDialogHide
    If Job.Success Then
        Dim res As String
        res=Job.GetString
        Log("Server response: " & res)
        Dim parser As JSONParser
        parser.Initialize(res)
     
        Select Job.JobName
            Case "sumarcash"
                Dim l As List
                l= parser.NextArray
                If l.Size = 0 Then
                    lblTotaC.Text="SIN DATOS"
                Else
                    Dim m As Map
                    m= l.Get(0)
                    lblTotaC.Text= m.Get("TotalesC")
                 
                End If
                                 
        End Select
     
    Else
        ToastMessageShow("Error: " & Job.ErrorMessage,True)
    End If
     
    Job.Release
End Sub

If it is necessary to remake the code there are no problems. I just want to bring that value from the mysql table to my label in the PAGOS (PAYMENTS) Activity Module. I have tested the php file with "POSTMAN" to see if the query into the php file brings this value and actually pulls it. I think I have the problem in B4A
 

OliverA

Expert
Licensed User
Longtime User
remoteQuery("SELECT SUM(monto) AS TotalesF FROM gastoscash", "sumcash")
Here you call remoteQuery with a nombreJob of "sumcash", yet in the PHP script you are looking for "sumarcash" and your JobDone method is looking for a JobName of "sumarcash". So I think you have your call to remoteQuery set wrong and it should be
B4X:
remoteQuery("SELECT SUM(monto) AS TotalesF FROM gastoscash", "sumarcash")
Note: I have no clue about your PHP "sumarcash:" line, since nowhere in your B4A job.Poststring call do you pass that information to PHP.
 
Upvote 0

Daniel44

Active Member
Licensed User
Here you call remoteQuery with a nombreJob of "sumcash", yet in the PHP script you are looking for "sumarcash" and your JobDone method is looking for a JobName of "sumarcash". So I think you have your call to remoteQuery set wrong and it should be
B4X:
remoteQuery("SELECT SUM(monto) AS TotalesF FROM gastoscash", "sumarcash")
Note: I have no clue about your PHP "sumarcash:" line, since nowhere in your B4A job.Poststring call do you pass that information to PHP.
Hey Oliiver... yes you're right it isn't same but that's just a tag as parameter... i did that example but without a great knowledge about that... jeje... I tried a lot of things... before this I just call by jobname a php script to sum everything.. I mean the sum was done on the server side... it didnt work.. in this example I do the sum via device side.. it didnt work either.... I tried with a lot examples here in the forum.. my God... a pretty little thing ... and I cant do it... well Im still reading books forums... looking for information.. Thank you
 
Upvote 0

Daniel44

Active Member
Licensed User
Here you call remoteQuery with a nombreJob of "sumcash", yet in the PHP script you are looking for "sumarcash" and your JobDone method is looking for a JobName of "sumarcash". So I think you have your call to remoteQuery set wrong and it should be
B4X:
remoteQuery("SELECT SUM(monto) AS TotalesF FROM gastoscash", "sumarcash")
Note: I have no clue about your PHP "sumarcash:" line, since nowhere in your B4A job.Poststring call do you pass that information to PHP.
I posted upper Php sumarcash
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
But did you make the change from "sumcash" to "sumarcash"? Once you do that, what does your JobDone method log?
 
Upvote 0

Daniel44

Active Member
Licensed User
Hey Oliver I decided re-make the whole code and Im on that yet I was in troubles coding server side. Now Im coding on the client side but coding different logical way. Thank you.
 
Upvote 0
Top