Android Question how to retrieve dataset returned from web application

Sofiya

Member
Licensed User
B4X:
 public class OutletController : Controller
    {
        // GET: Outlet
        public ActionResult Index()
        {
            return View();
        }

      
        public DataSet GetPosname()
        {
        
            using (SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=lpos;user id=sa;password=123;MultipleActiveResultSets=True;"))
            {
                using (SqlCommand cmd = new SqlCommand("select * from posmast", con))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                        return ds;
                    }
                }
            }
        }
    }

above visual c# .net function is my web application to retrieve data from database, here it returns record set, but in the web browser it throw the output as

System.Data.DataSet

i am not sure whether its returning correct dataset or it showing wrong output

if its correct output in browser then how to use this record set in our B4A project to read all the rows in the recordset
 

MarkusR

Well-Known Member
Licensed User
Longtime User
at android i would expect a json output (input), from pc it should be a returned xml.
i used vs 2017 with web api project, there is uses a ApiController not Controller
B4X:
public class BarcodeController : ApiController

example class that work with return
B4X:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VV_Web_API
{
    public class Barcode
    {
        public string barcode = "123";
        public string marking = "marking";
        public string error = "";

    }
}

api
B4X:
        //   http://localhost:52407/api/barcode/1?barcode=0108000000008&customer=0
        public Barcode GetById(int id, string barcode = "",string customer="")
        {

            Barcode bc = new Barcode();
         //...
            return bc;
        }
 
Last edited:
Upvote 0

Sofiya

Member
Licensed User
at android i would expect a json output (input), from pc it should be a returned xml.
the returned class must be serializable.

could u please give small example or json syntax , it will be very useful for me and for pc how can i serialize returned class
Thanks in advance
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
see above - the serialize is an automatic there.
and here the part in b4a
B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

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.

    Private LabelBC As Label
    Private LabelError As Label
    Private LabelMarking As Label
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("Marking")
  
    Dim BC As String
    BC = Main.ScannedBarcode
  
    Dim Job As HttpJob
    Job.Initialize("Job1",Me)
    Job.Username="..."
    Job.Password="..."
    Job.Download("https://xy.xy.com/api/barcode/1?barcode=" & BC & "&customer=kro")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub JobDone (Job As HttpJob)
  
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "Job1"

                Dim JSON As JSONParser
                JSON.Initialize(Job.GetString)

                Dim Map1 As Map
                Map1 = JSON.NextObject
              
                LabelBC.Text = Map1.Get("barcode")
                LabelMarking.Text = Map1.Get("marking")
                LabelError.Text = Map1.Get("error")
              
                'print the result to the logs
                Log(Job.GetString)
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
  
End Sub
 
Upvote 0
Top