Android Question How can i create a generic handler to get json response from api

Makumbi

Well-Known Member
Licensed User
Please help how can i create a generic handler using vb to receive the response from the API after the transaction is successful

the URL should be include here
B4X:
    Subscriptiondetails.put("status_notification_url","needed generic handler here to receive response posted by the api")

My problem is how can i create that generic handler to automatically get this json response so that i can use it to find wether the transaction was successfull or not below is the response that is sent by the api after a successful execution thanks in advance. Note this notification changes based on the phone number that has sent the request


Below is an example of the notification sent when the transaction is successful.
B4X:
{"status": "SUCCESSFUL", "msisdn": "256782123456", "initiation_date": "2019-10-21 09:03:03","completion_date": "2019-10-21 09:03:22", "amount": 20000, "receipt_number":  "1587906379","reference_code": "c1a943f6-3f1aa162-95434a95-ef93f253-1aa43dc7"}


B4X:
Dim Subscriptiondetails As Map
                Subscriptiondetails.Initialize
    
                Subscriptiondetails.put("username","ggg")
                Subscriptiondetails.put("password", "ggg")
                Subscriptiondetails.put("api","depositmobilemoney")
                Subscriptiondetails.put("msisdn","256782911364")
                Subscriptiondetails.put("amount",1000)
                Subscriptiondetails.put("narration","Subscription")
                Subscriptiondetails.put("reference","sbscr")
                'Subscriptiondetails.put("status_notification_url","URl")
                Subscriptiondetails.put("status_notification_url","needed generic handler here to recieve reponse posted by the api")
      
                'Depends on json library
                Dim json As JSONGenerator
                json.Initialize(Subscriptiondetails)
                Dim content As String = json.ToString
        
                'Depends on okHttputils library
                Dim client As HttpJob
                client.Initialize("",Me)
        
                client.PostString("https://payments-dev.blink.co.ug/api/",content)
                client.GetRequest.SetContentType("application/json")    'set the header as json
                client.GetRequest.SetContentEncoding("UTF8")        'set encoding as utf8
        
                Wait For Jobdone(client As HttpJob)
    
                If client.Success Then
                    Dim Resultd As String = client.GetString
                    Dim data As JSONParser
                    data.Initialize(Resultd)
        
                    Dim product As Map = data.NextObject    'I suspect the response is a json object
        
                    Dim y As String = product.Get("status")
                    Dim z As String = product.Get("error")
                    Dim w As  String = product.Get("reference_code")
        
                    If y = "PENDING" And z = "false" Then
                        MsgboxAsync("Hey!, Yr subscription IS BEING WORKED UPON THANK YOU","Alright")
                    End If
        
                End If

        
                End If
        End If
    End If
End If
 
Top