B4J Question Web Server & Web Service

ziomorgan

Member
Licensed User
If i want to wait BitlyAnalytics_Result it is this correct ?

B4X:
Public Sub Handle(req As ServletRequest, resp As ServletResponse)
    StartMessageLoop
    Try
        BITLY.Parent = Me
        BITLY.AccessToken = Main.AccessTokenBitly
        BITLY.BitlyAnalytics(sRecord(LINKBITLY))
    Catch
        resp.ContentType = "application/json; charset=UTF-8"
        resp.Write(Main.GeneraErrorJson(500,"Database Access Error"))
        resp.Status = 500
    End Try
End Sub

Sub BitlyAnalytics_Result(Result As String, Success As Boolean)
    StopMessageLoop
    If Success = True Then
        mResponse.Initialize
        mResponse.Put("responseCode", 0)
        mResponse.Put("responseDescription", "Successfully")
        jsonError.Initialize(mResponse)
        respGlobal.ContentType = "application/json"
        respGlobal.Write(jsonError.ToString)
        respGlobal.Status = 200
    Else
        respGlobal.ContentType = "application/json; charset=UTF-8"
        respGlobal.Write(Main.GeneraErrorJson(400,Result))
        respGlobal.Status = 400
    End If
End Sub
 
Last edited:

ziomorgan

Member
Licensed User
B4X:
Public Sub Handle(req As ServletRequest, resp As ServletResponse)
   ' si memorizza l'oggetto resp servlet
   respGlobal = resp
   Try
     BitlyAnalytics(req.GetParameter("link_to_check"))
  Catch
     resp.ContentType = "application/json; charset=UTF-8"
     resp.Write(Main.GeneraErrorJson(500,"Handle Error"))
     resp.Status = 500
   End Try
   StartMessageLoop
End Sub

Public Sub BitlyAnalytics(ShortUrlToCheck As String)
   Dim hjAnalytics As HttpJob
    Try
       hjAnalytics.Initialize("hjAnalytics", Me)
         hjAnalytics.Download2(EncodeUrl(UrlAnalytics), Array As String("access_token", AccessToken, "link", EncodeUrl(ShortUrlToCheck)))
       hjAnalytics.GetRequest.Timeout = 10000
       hjAnalytics.Tag = ShortUrlToCheck
     Catch
         log("ERRORE DURANTE LA RICHIESTA ANALYTICS A BITLY. RIPROVARE")
      End Try
End Sub

Private Sub JobDone (Job As HttpJob)
   If Success = True Then
       Dim mResponse As Map
       Dim jsonError As JSONGenerator
       jsonError.Initialize2(lRequests)
       mResponse.Initialize
       mResponse.Put("responseCode", 0)
       mResponse.Put("responseDescription", "Query Successfully")
       mResponse.Put("Requests", jsonError.ToString)
       Log(jsonError.ToString)
       jsonError.Initialize(mResponse)
       Log(jsonError.ToString)
       respGlobal.ContentType = "application/json"
       respGlobal.Write(jsonError.ToString)
       respGlobal.Status = 200
   Else
     respGlobal.ContentType = "application/json; charset=UTF-8"
     respGlobal.Write(Main.GeneraErrorJson(400,Result))
     respGlobal.Status = 200
   End If
   Job.Release
     StopMessageLoop
End Sub
 
Top