Android Question Send JSON to MySQL-Solved

erasmusackon

Member
Licensed User
Longtime User
Please what am i doing wrong in this code.

Am getting error message that "JSON array expected". The error occurs in jobdone.
I was expecting that once the application get to the Jobdone i was expecting the data to be in Mysql database but there's nothing in there

B4X:
    Dim iMap As Map
    iMap.Initialize
   
    Dim JSONList As List
    JSONList.Initialize

sel=SQL1.ExecQuery("SELECT * FROM DB_TransactionsDaily WHERE AgentID='" & LogIn_MobilizerID & "' AND Upload='" & Upload & "'")
        sel.Position=0
        recNo=sel.RowCount
       
        ProgressDialogShow2("Exporting Deposit Transactions from Phone to server. Please Wait...",False)
       

If sel.RowCount>0 Then
           
            For i = 0 To sel.RowCount-1
                                           
                sel.Position=i
                iMap.put("AgentID", sel.GetString("AgentID"))
                iMap.put("Cr", sel.GetString("Cr"))
                iMap.put("Inputer", sel.GetString("Inputer"))
               
                iMap.put("MemberID", sel.GetString("MemberID"))
                iMap.put("ProID", sel.GetString("ProID"))
               
                iMap.put("TransDetails", sel.GetString("TransDetails"))
                iMap.put("AutoID", sel.GetString("AutoID"))
               
            Next 
           
            JSONList.add(iMap)
            sel.Close
           
            Dim JSONGenerator As JSONGenerator
              JSONGenerator.Initialize2(JSONList)   
           
            Dim JSONstring As String
              JSONstring = JSONGenerator.ToString
           
            Try
                Dim DownloanTransactions As HttpJob
                DownloanTransactions.Initialize("Export Data", Me)
                DownloanTransactions.PostString(Server, Array As String ("action","JSONTRANS","MyJSON",JSONstring))
               
            Catch
                Msgbox(LastException.Message,"Error")
                Log(LastException)
            End Try
           
            ProgressDialogHide
        End If
 

erasmusackon

Member
Licensed User
Longtime User
Jobdone

B4X:
Select Job.JobName
                       
                              
            Case "Export Data"
                Dim Transactions As List= parser.NextArray 'returns a list with maps

                RecordCount=Transactions.Size
                               
                If Transactions.Size>0 Then
                   
                For i = 0 To Transactions.Size - 1
                       
                        Dim TransList As Map
                        Dim sTransNo As String 
                       
                        TransList = Transactions.Get(i)                                   
                        sTransNo=TransList.Get("TransNo")
                       
                        Try
                            If sTransNo>0 Then

                                Dim nQuery As String
                                nQuery = "UPDATE DB_TransactionsDaily SET Upload=1 WHERE AutoID ='" & sTransNo & "'"
                                SQL1.ExecNonQuery(nQuery)
                               
                            End If
                       
                        Catch
                            Log(LastException)
                        End Try
                       
                    Next
                   
                    Msgbox("Export completed","Export Data")
                   
                End If

PHP

B4X:
case "JSONTRANS":
        $json = $_GET["MyJSON"];
        $jsall = array();
        $jsone = array();
        $jsall=json_decode($json, true);
        $x = 0;
       
          while($x < count($jsall)) {
            $jsone=$jsall[$x];
         
            $agentID = $jsone["agentID"];
            $amount = $jsone["amount"];
            $inputer=$jsone["inputer"];
            $memberID = $jsone["memberID"];
            $proID = $jsone["proID"];
            $transDetails = $jsone["transDetails"];
            $transNo=$jsone["transNo"];
           
            $q = mysqli_query($con,"INSERT INTO db_transactions (AgentID, Amount,Inputer,MemberID,ProID,TransDetails,TransNo) VALUES ('$agentID', 
            $amount,'$inputer','$memberID','$proID','$transDetails','$transNo')");
           
           $x++;
        }
       
        $q = mysqli_query($con,"SELECT * FROM db_transactions WHERE inputer='$inputer' AND AgentID='$agentID'");
        $rows = array();
        while($r = mysqli_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
       
        mysqli_close($con);
     break;
 
Upvote 0

erasmusackon

Member
Licensed User
Longtime User
Where is the code that initializes parser? What is the output of Log(Job.GetString)?
Job.GetString :
<br />

<b>Notice</b>: Undefined index: action in <b>C:\xampp\htdocs\app\newcon.php</b> on line <b>24</b><br />

This is line 24 : $action = $_POST["action"];

I have changed to $_GET["action"]; but still getting the same error

B4X:
<?php

$databasehost = "localhost";
$databasename = "ssisldb";
$databaseusername = "root";
$databasepassword = "";

$con=mysqli_init();


if (!$con)
  {
  die("mysqli_init failed");
  }

if (!mysqli_real_connect($con,$databasehost,$databaseusername,$databasepassword,$databasename))
 {
 die("Connect Error: " . mysqli_connect_error());
 }

mysqli_ssl_set($con,"key.pem","cert.pem","cacert.pem",NULL,NULL);
mysqli_set_charset($con,"utf8");

$action = $_POST["action"];

switch ($action)

{ 
    Case "GetFieldUsers":
        $q = mysqli_query($con,"SELECT * FROM users");
        $rows = array();
        while($r = mysqli_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
        mysqli_close($con);
     
    Case "GetProduct":
        $q = mysqli_query($con,"SELECT * FROM Product_Setup");
        $rows = array();
        while($r = mysqli_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
        mysqli_close($con);
     
    case "GetComp":
        $q = mysqli_query($con,"SELECT * FROM CompInfo");
        $rows = array();
        while($r = mysqli_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
        mysqli_close($con);
    break;
 
    Case "GetAccounts":
        $q = mysqli_query($con,"SELECT * FROM Account");
        $rows = array();
        while($r = mysqli_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
        mysqli_close($con);
    break;
 
    case "NewTrans":
        $agentID = $_GET["agentID"];
        $amount = $_GET["amount"];
        $inputer=$_GET["inputer"];
        $memberID = $_GET["memberID"];
        $proID = $_GET["proID"];
        $transDetails = $_GET["transDetails"];
        $transNo=$_GET["transNo"];
   
        $q = mysqli_query($con,"INSERT INTO db_transactions (AgentID, Amount,Inputer,MemberID,ProID,TransDetails,TransNo) VALUES ('$agentID',
                          $amount,'$inputer','$memberID','$proID','$transDetails','$transNo')");
             
        $q = mysqli_query($con,"SELECT * FROM db_transactions WHERE TransNo='$transNo' AND AgentID='$agentID'");
        $rows = array();
        while($r = mysqli_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
     
        mysqli_close($con);
     break;
 
     case "NewCustomer":
        $accountNumber = $_GET["accountNumber"];
        $agentName = $_GET["agentName"];
        $boxaddress=$_GET["boxaddress"];
        $customer_name = $_GET["customer_name"];
        $memberid = $_GET["memberid"];
        $mobilizerid = $_GET["mobilizerid"];
        $occupation=$_GET["occupation"];
        $phoneno=$_GET["phoneno"];
        $resaddress=$_GET["resaddress"];
        $userid=$_GET["userid"];
 
        $q = mysqli_query($con,"INSERT INTO customer_records (AccountNumber,AgentName,BoxAddress,Customer_Name,MemberID,MobilizerID,Occupation,PhoneNo,ResAddress,UserID) VALUES ('$accountNumber',
                          $agentName,'$boxaddress','$customer_name','$memberid','$mobilizerid','$occupation','$phoneno','$resaddress','$userid')");
             
        $q = mysqli_query($con,"SELECT Auto_ID,MemberID FROM customer_records WHERE MemberID='$memberid' AND MobilizerID='$mobilizerid'");
        $rows = array();
        while($r = mysqli_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
     
        mysqli_close($con);
     
    break;
 
    case "NewAccount":
        $proid = $_GET["proid"];
        $memberid = $_GET["memberid"];
        $inputer=$_GET["inputer"];
        $depositrate=$_GET["depositrate"];
     
        $q = mysqli_query($con,"INSERT INTO new_account (ProID, MemberID,Inputer,DepositRate) VALUES ('$proid', '$memberid','$inputer',$depositrate)");
     
        $q = mysqli_query($con,"SELECT MemberID,ProID FROM new_account where MemberID='$memberid' AND ProID='$proid'");
        $rows = array();
        while($r = mysqli_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
     
        mysqli_close($con);
    break;
 
   case "JSONTRANS":
        $json = $_GET["MyJSON"];
        $jsall = array();
        $jsone = array();
        $jsall=json_decode($json, true);
        $x = 0;
     
          while($x < count($jsall)) {
            $jsone=$jsall[$x];
       
            $agentID = $jsone["agentID"];
            $amount = $jsone["amount"];
            $inputer=$jsone["inputer"];
            $memberID = $jsone["memberID"];
            $proID = $jsone["proID"];
            $transDetails = $jsone["transDetails"];
            $transNo=$jsone["transNo"];
         
            $q = mysqli_query($con,"INSERT INTO db_transactions (AgentID, Amount,Inputer,MemberID,ProID,TransDetails,TransNo) VALUES ('$agentID',
            $amount,'$inputer','$memberID','$proID','$transDetails','$transNo')");
         
           $x++;
        }
     
        $q = mysqli_query($con,"SELECT * FROM db_transactions WHERE inputer='$inputer' AND AgentID='$agentID'");
        $rows = array();
        while($r = mysqli_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        print json_encode($rows);
     
        mysqli_close($con);
     break;
}
?>
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…