B4J Question Web Server & Web Service

Discussion in 'B4J Questions' started by ziomorgan, May 15, 2015.

  1. ziomorgan

    ziomorgan Member Licensed User

    I need to call a web service from within a web server.
    It's possible?
    How?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Yes. With HttpUtils2. You should use StartMessageLoop in the handler and call StopMessageLoop when the request completes.
     
  3. ziomorgan

    ziomorgan Member Licensed User

    If i want to wait BitlyAnalytics_Result it is this correct ?

    Code:
    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: May 18, 2015
  4. Erel

    Erel Administrator Staff Member Licensed User

    I don't think that this code is correct. However I'm not sure that I understand it.

    Where is the HttpJob request?
     
  5. ziomorgan

    ziomorgan Member Licensed User

    In the Handle sub
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    I don't see it in the code you posted.
     
  7. ziomorgan

    ziomorgan Member Licensed User

    Code:
    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
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    The code seems to be correct. Make sure not to break the url in your EncodeUrl method.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice