iOS Question Extract HTTP Response on Error

iCAB

Well-Known Member
Licensed User
Longtime User
Hello There

I am using HTTPUtils ( https://www.b4x.com/android/forum/threads/46565/#content ) and so far all is good until I tried to extract the response block returned OnError by the server ( I was unable to find a way to do it unless I am missing something).

B4X:
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
'Unable to get the info from Response on error
End Sub

When I examined our equivalent B4A code, I noticed that we are using OKHTTPResponse.
With B4I, it seems that I have to include the standard library iHttpUtils2 while with B4A I didn't (again if I am not wrong in here). But when I include iHttpUtils2 the compiler complains about a duplicate declaration for HttpJob because we are using a shared module named "HttpJob" between the B4A and B4I apps.

What is the best way around this without having to rename the shared module
 

Semen Matusovskiy

Well-Known Member
Licensed User
Take a look a folder, where B4i is installed. Modify iHttpUtils2.b4xlib inside Libraries subfolder.
b4xlib is a zip file. Simply change extension and extract bas files.
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Hi All,

Thanks for all your help, but somehow I guess we moved away from the initial problem that I was trying to solve in the first place. Perhaps my mistake.

My main goal is to be able to extract the response contents when the server returns an Error.

With B4A, Response.ErrorResponse holds everything I need
B4X:
Sub hc_ResponseError (Response As OkHttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    Dim ServerResponse as String = Response.ErrorResponse

    'I am able to decode the JSON string returned by the server in B4A
    
End Sub


In B4I, the response signature

B4X:
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    'How do I get the response contents in here.
    'The server is returning a JSON block with information messages
End Sub


Thanks
iCAB
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
I remember this problem. When I wrote about 3 years ago, I asked to replace error code to 250 (pseudo-success). Web developer was very angry - he likes standards.

But then I read that Erel fixed this trouble. If to compare old and current release, a new release tries to replace Reason by Response.GetString.
B4X:
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    Log("ResponseError: " & Reason & ", status code: " & StatusCode)
    Try
        Dim j As String = Response.GetString
        If j <> "" Then Reason = j
    Catch
        Reason = "(Error decoding response)"
    End Try
    CompleteJob(TaskId, False, Reason, Response)
End Sub
Try to correct your HttpUtils2Service.Bas. Probably, will work.
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Thanks Semen,

With the version I am using, it says that GetString should only be used inside "ResponseSuccess"

1630367145777.png


Try to correct your HttpUtils2Service.Bas. Probably, will work.
Is this fixed in a newer release?
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
iHttp should be current (1.02).
The text - I think that simply forgot to change xml-file.
 

Attachments

  • HttpJob.bas
    10.6 KB · Views: 61
  • HttpUtils2Service.bas
    4.1 KB · Views: 56
Last edited:
Upvote 0
Top