Android Question How to create a API REST for Connecting B4A with Wordpress

FabianGS

Member
Ey Guys Looking for some help here im trying to connect my B4A APP with Wordpress. According to this post REST API I got this working correctly on Python but translated to VB is something like this:

ButtonWordpress_Click:
    Dim J As HttpJob
    Dim Data As Map = CreateMap("title": "DCA DB", "content": "HELLO FROM BASIC 4 ANDROID", "status": "publish")
    Dim Json As String = Data.As(JSON).ToString
    Dim Response As String
    J.Initialize("", Me)
    J.Username = "Fabian"
    J.Password = "XXXXXXXXXXXXXXXXXXXXX"
    
    J.PostString("https://dca-mx.com/wp-json/wp/v2/pages/362", Json)
    
    Wait for (J) JobDone(J As HttpJob)
    If J.Success Then
        Response = J.PostString("https://dca-mx.com/wp-json/wp/v2/pages/362", Json)
        Log(Response)
        Log(J.GetString)
    End If

I have this error when run this:
ResponseError. Reason: okhttp3.internal.http2.StreamResetException: stream was reset: PROTOCOL_ERROR, Response:
 
Solution
We are writing in B4X code here, not VB.
There are 2 PostString in the code.
The response is expected to be a Map
B4X:
Dim response As Map = job.GetString.As(JSON).ToMap

B4X:
Sub UpdatePage
    Try
        Dim Auth As String = "Fabian:xxxx xxxx xxxx xxxx xxxx xxxx"
        Dim SU As StringUtils
        Dim Encoded As String = SU.EncodeBase64(Auth.GetBytes("UTF8"))
        Dim job As HttpJob
        job.Initialize("", Me)
        Dim Data As Map = CreateMap("title": "DCA DB", "content": "HELLO FROM BASIC 4 ANDROID", "status": "publish")
        Dim json As String = Data.As(JSON).ToString
        job.PostString("https://dca-mx.com/wp-json/wp/v2/pages/362", json)
        job.GetRequest.SetHeader("Authorization", "Basic " & Encoded)...

aeric

Expert
Licensed User
Longtime User
We are writing in B4X code here, not VB.
There are 2 PostString in the code.
The response is expected to be a Map
B4X:
Dim response As Map = job.GetString.As(JSON).ToMap

B4X:
Sub UpdatePage
    Try
        Dim Auth As String = "Fabian:xxxx xxxx xxxx xxxx xxxx xxxx"
        Dim SU As StringUtils
        Dim Encoded As String = SU.EncodeBase64(Auth.GetBytes("UTF8"))
        Dim job As HttpJob
        job.Initialize("", Me)
        Dim Data As Map = CreateMap("title": "DCA DB", "content": "HELLO FROM BASIC 4 ANDROID", "status": "publish")
        Dim json As String = Data.As(JSON).ToString
        job.PostString("https://dca-mx.com/wp-json/wp/v2/pages/362", json)
        job.GetRequest.SetHeader("Authorization", "Basic " & Encoded)
        job.GetRequest.SetContentType("application/json")
        Wait For (job) JobDone (job As HttpJob)
        If job.Success Then
            Dim response As Map = job.GetString.As(JSON).ToMap
            For Each key As String In response.Keys
                Log($"${key}: ${response.Get(key)}"$)
            Next
        Else
            Log(job.ErrorMessage)
        End If
    Catch
        Log(LastException)
    End Try
    job.Release
End Sub
 
Upvote 1
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…