Android Question Get a String from a OKHttpResponse

Jakes72

Active Member
Licensed User
Longtime User
Hi Experts,

I am back with another interesting pop quiz, which I am sure an expert can solve in 5 seconds! :)

You see I am trying to get a string (The Server just answers back 'Hello World!') from a Response_Success event like this:

B4X:
Private Sub HTTPClient_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
    
    Dim os As OutputStream
    os.InitializeToBytesArray(100)
        
    'Write response as a text file.
    'This will work, but trying to avoid this extra step.
    'Response.GetAsynchronously("Response",File.OpenOutput(File.DirInternalCache,"Response.txt",False),True,TaskId)
    
    Response.GetAsynchronously("Response",os,True,TaskId)
    Dim DataBytes() As Byte = os.ToBytesArray
    Dim strTemp As String = BytesToString(DataBytes,0,DataBytes.Length,"UTF8")
    
End Sub

Now I get no errors, but my DataBytes & strTemp variables are blank.
The os buffer is full of data though, that I can see.

But If I go one step further and write the Response to a text file and then use:

B4X:
Private Sub Response_StreamFinish(Success As Boolean,TaskID As Int)
     Dim strTemp As String = File.ReadString(File.DirInternalCache, "Response.txt")
End Sub

Then I will see my 'Hello World' in strTemp.

What am I am doing wrong? :(

Regards,
Jacques.
 

TILogistic

Expert
Licensed User
Longtime User
You simply have to use:

see:
 
Last edited:
Upvote 0

Jakes72

Active Member
Licensed User
Longtime User
Hi Oparra, I am using OKHttpUtils2.

Everything is working 100%, but there is obviously something I am doing wrong in this code:

B4X:
    Dim os As OutputStream
    os.InitializeToBytesArray(100)
        
    'Write response as a text file.
    'This will work, but trying to avoid this extra step.
    'Response.GetAsynchronously("Response",File.OpenOutput(File.DirInternalCache,"Response.txt",False),True,TaskId)
    
    Response.GetAsynchronously("Response",os,True,TaskId)
    Dim DataBytes() As Byte = os.ToBytesArray
    Dim strTemp As String = BytesToString(DataBytes,0,DataBytes.Length,"UTF8")

Basically all I want to know is how to get the Output of the Response into a text string instead of a File.
 
Upvote 0

mcqueccu

Well-Known Member
Licensed User
Longtime User
Upvote 0

Jakes72

Active Member
Licensed User
Longtime User
You see the reason why I am doing it this way is because I want to wrap my code in a Class module and I could not get the 'JobDone' event to fire in my class module.

But I have figured out what I was doing wrong, basically I was jumping the gun, I was trying to populate my OutputStream Object (os) BEFORE the Response_StreamFinish event had fired.

I just moved this code into the 'Response_StreamFinish' event and now it works :)

So my code now looks like this:

B4X:
Private Sub HTTPClient_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
    
    mOS.InitializeToBytesArray(100)           'mOS is a Global Variable of type OutputStream.  
   Response.GetAsynchronously("Response",mOS,True,TaskId)
   
End Sub


Private Sub Response_StreamFinish(Success As Boolean,TaskID As Int)
    
        Dim DataBytes() As Byte = mOS.ToBytesArray        
        Dim strTemp As String = BytesToString(DataBytes,0,DataBytes.Length,"UTF8")
    
End Sub

So actually for anyone wanting to know this is kind of like the equivalent of 'GetString' when you use the code like in the:
https://www.b4x.com/android/forum/threads/b4x-okhttputils2-with-wait-for.79345/

Thanks guys for the help, you pushed me in the right direction.
 
Upvote 0

Jakes72

Active Member
Licensed User
Longtime User
Hi Erel,

Eureka! As usual you are absolutely correct!
I had another careful look at my code and the error was that I was initialising the HttpJob object like this:

B4X:
mHttp.Initialize("HTTPClient",mCaller)    '<--- mCaller is the Main Activity this is wrong!

I should have done this:
B4X:
mHttp.Initialize("HTTPClient",Me)           '<--- Ref to the Class Moduel instead

It now works great andthe 'JobDone' fires!
Thanks again everyone for helping me in the right direction, sometimes these small errors are hard to find!
 
Upvote 0
Top