Android Question wait for Jobdone synchronous

Charles Biba

Member
Licensed User
I am using the ZXing library to scan a barcode which returns in the zx1_scan_result () routine. I currently call a subroutine in my activity that displays the scan result in a text field. However, what I need to accomplish is to take the scantext value, launch an httpjob to do an SQL query and convert the UPC into a partnumber. I have the httpjob working individually, but I am having trouble making this work in a more synchronous manner. Ex. Not execute CallSubDelayed2 (StockInquiry, "StockInquiryScanValue", partnumber) until ConvertUPCToPart(scantext) returns a value.

ConvertUPCToPart() routine is dead simple based on the examples, but the result is generated in the Jobdone event which is obviously asynchronous. I get the result from the web query, but I need to link the two so the code waits for the result. I am thinking the waitfor structure would work, but I am uncertain how best to return the value. Should be in the same activity as the Zxing routines? Should it be in a class?






B4X:
Sub zx1_scan_result (scantext As String, scanformat As String)
 

 Dim rtn As String
 
 zx1.stopScanner
 
 Select CallingActivity
 
  Case "StockInquiry"

   partnumber =  ConvertUPCToPart(scantext)
  
   CallSubDelayed2 (StockInquiry, "StockInquiryScanValue", partnumber)

  Case "LocationShelfInquiry"
   CallSubDelayed2 (LocationShelfInquiry, "LocationInquiryScanValue", scantext)

  Case Else ' If scanner is called directly, show the decode in a msgbox and exit.
   zx1.Visible = False
  
   If CallingActivity = "" Then
   
    Msgbox (scantext, "UPC VALUE")
   
   End If
  
   Activity.Finish
 
 
 End Select
 
End Sub


The HTTP web routine:

B4X:
Sub ConvertUPCToPart(strUPC As String)
 
 
 Dim sSQL As String
 Dim sSQL2 As String
 
 Dim job1 As HttpJob

 ' Job 1 Lookup Secondary Barcode
 sSQL = ""
 sSQL = sSQL & " Select subkey1 As [Part], [Autopart].[dbo].[product].[desc] As [Desc],A12 As [UPC],[Autopart].[dbo].[product].[range] As [Range], Qty As [BoxQty] "
 sSQL = sSQL & " FROM [Autopart].[dbo].[Mvpr], [Autopart].[dbo].[product] where SubKey1 = [Autopart].[dbo].[product].[keycode] "
 sSQL = sSQL &  " And Prefix = 'L' and A12 = '~UPC~' "
 
 
 If strUPC <> "" Then
  sSQL2 = sSQL.Replace ("~UPC~",strUPC)
 Else
  ' Do nothing
  Return
 End If

 
 ' Secondary barcode lookup
 job1.Initialize("Job1", Me)
 job1.PostString(ServerUrl, sSQL2)
 
End Sub

Sub JobDone (Job As HttpJob)
 If Job.Success Then
  Dim parser As JSONParser
  Dim response As String = Job.GetString
  parser.Initialize(response)
  Dim rows As List
  rows = parser.NextArray
 
  If rows.Size = 0 Then
  
   Msgbox ("No Results","Search")
   Return
  
  End If
 
 
  'work with result
  'rows is a List. Each item is a Map with the columns names as keys and the db values as the values.
  For i = 0 To rows.Size - 1
   Log("Rows #" & i)
   Dim m As Map
   m = rows.Get(i)
   Log("item1=" & m.Get("Part")) 'log the values of col1 and col2
   Log("item2=" & m.Get("Desc"))
   Log("item2=" & m.Get("BoxQty"))
  
  Next
 End If

End Sub
 

Charles Biba

Member
Licensed User
Ok... I re-wrote my subroutine to do the httpjob inline, if that is the correct term. I should be able to leverage resumeable sub to accomplish this, but I cannot get it to work. help!

B4X:
Sub ConvertUPCToPart (strUPC As String)  as resumablesub
 
 
 ' verify the user credentials against the database. Query for username and password
 
 Dim sSQL As String
 Dim sSQL2 As String
 
 Dim job As HttpJob

 ' Job 1 Lookup Secondary Barcode
 sSQL = ""
 sSQL = sSQL & " Select subkey1 As [Part], [Autopart].[dbo].[product].[desc] As [Desc],A12 As [UPC],[Autopart].[dbo].[product].[range] As [Range], Qty As [BoxQty] "
 sSQL = sSQL & " FROM [Autopart].[dbo].[Mvpr], [Autopart].[dbo].[product] where SubKey1 = [Autopart].[dbo].[product].[keycode] "
 sSQL = sSQL &  " And Prefix = 'L' and A12 = '~UPC~' "
 
 
 If strUPC <> "" Then
  sSQL2 = sSQL.Replace ("~UPC~",strUPC)
 Else
  ' Do nothing
  Return
 End If
 
 ' Job 2 Lookup Primary Barcode
 'sSQL = ""
 'sSQL = sSQL &  "SELECT KeyCode as [Part],[Desc],Asno,[Range],'1' as [DefaultQty] "
 'sSQL = sSQL &  "FROM [Autopart].[dbo].[Product] where Asno = '~UPC~' "
 
 ' Send Job 1, Secondary barcode lookup
 job.Initialize("Job1", Me)
 job.PostString(ServerUrl, sSQL2)
 
 Wait For (job) JobDone(job As HttpJob)
 If job.Success Then
  Dim parser As JSONParser
  Dim response As String = job.GetString
  parser.Initialize(response)
  Dim rows As List
  rows = parser.NextArray
  
  'work with result
  'rows is a List. Each item is a Map with the columns names as keys and the db values as the values.
  For i = 0 To rows.Size - 1
   Log("Rows #" & i)
   Dim m As Map
   m = rows.Get(i)
   Log("item1=" & m.Get("Part")) 'log the values of col1 and col2
   Log("item2=" & m.Get("Desc"))
   Log("item2=" & m.Get("BoxQty")

   return m.Get("Part")

  Next
 End If

End Sub


Calling routine (snip):

B4X:
   partnumber =  ConvertUPCToPart(scantext)
 
   CallSubDelayed2 (StockInquiry, "StockInquiryScanValue", partnumber)
 
Upvote 0
Top