Android Question HttpUtils - Resolved.

padvou

Active Member
Licensed User
Longtime User
I had a project which ran fine on previous versions of B4A.
Now I loaded it with version 6.80.
There were some errors with http related code, so I removed the bas modules related with http and loaded libraries okhttp (1.20) and okhttputils (2.20). I modified the relevant code to okhttp...
However there's some code that needs to be like this:
B4X:
Sub Http_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
   
    Dim gotstring As String
    gotstring=Response.GetString("UTF8")
....
end sub

I've seen other posts that getstring is not supported, but I am confused. How do I read the response's string?
Thank you
 

KMatle

Expert
Licensed User
Longtime User
Example:

B4X:
Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   If Job.Success = True Then
      Select Job.JobName
         Case "Job1", "Job2"
            'print the result to the logs
            Log(Job.GetString)
         Case "Job3"
            'show the downloaded image
            Activity.SetBackgroundImage(Job.GetBitmap)
      End Select
   Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
   End If
   Job.Release
End Sub

JobDone is called automatically when the job is finished.

With Job.GetString you usually get the response (except you expect other content). See the methods of "Job"
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
Example:

B4X:
Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   If Job.Success = True Then
      Select Job.JobName
         Case "Job1", "Job2"
            'print the result to the logs
            Log(Job.GetString)
         Case "Job3"
            'show the downloaded image
            Activity.SetBackgroundImage(Job.GetBitmap)
      End Select
   Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
   End If
   Job.Release
End Sub

JobDone is called automatically when the job is finished.

With Job.GetString you usually get the response (except you expect other content). See the methods of "Job"

Thank you for your response, but your code Dims the Job as HttpJob, whereas I use Okhttp. Isnt this a difference?
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
This is invalid code as you cannot get the stream synchronously.

It will take you 15 minutes to change to OkHttpUtils2 and it is worth the time...
Yes, of course I'm willing to take the time to switch to Okhttputils2.
I would just appreciate it very much if you could help me adjust my code:
B4X:
Sub btn_settingssave_Click
    Dim HttpResponse1 As OkHttpResponse
    Dim request As OkHttpRequest
    request.InitializeGet("http://................/status2.aspx?u=" & et_userid.Text & "&p=" & et_pass.text)
    request.Timeout=12000
   
    If HttpClient1.Execute(request, 1) = False Then Return
   
    ProgressDialogShow("Please wait...Checking credentials.")
    Else
   
    End If
End Sub



Sub Http_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
    ProgressDialogHide
    Dim gotstring As String
   
   
    gotstring=Response.GetString("UTF8")
   
   
    'Log(Response.GetString("UTF8"))
   

End Sub
 
Upvote 0

panagiotisden2

Active Member
Licensed User
Longtime User
B4X:
Sub btn_settingssave_Click
    Dim job1 As HttpJob

    job1.Initialize("job1",Me)
    job1.Download("http://................/status2.aspx?u=" & et_userid.Text & "&p=" & et_pass.text)
    job1.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
    job1.GetRequest.Timeout= 12000

    ProgressDialogShow("Please wait...Checking credentials.")
End Sub


Sub jobdone(job As HttpJob)
    Select job.JobName
        Case "job1"
          
            If job1.Success=False Then
                Log(job1.ErrorMessage)
                Return
            End If

            ProgressDialogHide
            Dim gotstring As String = gotstring=job1.GetString
           
            'Log(gotstring)
            Job1.release
    End Select
End Sub


hope it helps :)
 
Last edited:
Upvote 0

rzv25

Member
Licensed User
Longtime User
I think there is a difference between original code and the one converted to OkHttpUtils2.
The original one (using HTTP library) could be implemented in a code module, whereas the one converted to OkHttpUtils2 which uses a Job needs a reference to a object at Initialize phase, so it cannot be implemented in a code module.

Am I missing something ?
 
Upvote 0
Top