B4A Library OkHttp - Replaces the Http library

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
I use this code, to send notify, but the last 2 rows aren't correct:


B4X:
Dim job As HttpJob

job.Initialize("send message", Me)
job.PostString("https://android.googleapis.com/gcm/send", json.ToString)
job.GetRequest.SetContentType("application/json")
job.GetRequest.SetHeader("Authorization", "key=" & bsUtils.apikey)

The log error say:
B4X:
Error description: Unknown type: anywheresoftware.b4h.okhttp.OkHttpClientWrapper.OkHttpRequest
Are you missing a library reference?
 

StefanoAccorsi

Member
Licensed User
Longtime User
Hi, I need to send a login to a web service through a GET and not a POST. Please don't ask me why: I need to connect to an authentication server that uses this way.

So I tried a simple HttpJob and it is ok:

B4X:
http.Initialize("Login", Me)
http.Download("https://" & RERURL & "/xxx/xx/Login?goto=https%3A%2F%2F" & RERURL & "%2FMapper" & _
    "%2F&IDToken1=" & txtUsername.Text & "&IDToken2=" & txtPassword.Text)

the problem is that the login infos are stored in the header of the html I get as a response and so I can't use the simple Job.GetString: in it I find only the html "body" and not its headers.

So I switched to the more complex OkHttpClient but it doesn't work. I wrote:

B4X:
httpClient.InitializeAcceptAll("Login")
httpReq.InitializeGet("https://" & RERURL & "/xxx/xx/Login?goto=https%3A%2F%2F" & RERURL & "%2FMapper" & _
        "%2F&IDToken1=" & txtUsername.Text & "&IDToken2=" & txtPassword.Text)
httpClient.Execute(httpReq, 1000)

but when executed it never goes to the ResponseSuccess or ResponseError sub:

B4X:
Sub http_ResponseSuccess(Response As OkHttpResponse, TaskId As Int)
    ProgressDialogHide
    Dim listHeaders As List
    listHeaders = Response.GetHeaders.Get("Username")
    Log("Success")
    For i=0 To listHeaders - 1
        Log(listHeaders.Get(i))
    Next
End Sub

Sub http_ResponseError (Response As OkHttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    ProgressDialogHide
    Log("Error")
End Sub

So here my questions: there's a way to read the headers using the simpler HttpJob?
Otherwise, where's the error in the OkHttpClient implementation?

Thank you.
 

MarcoRome

Expert
Licensed User
Longtime User

Look THIS
 

StefanoAccorsi

Member
Licensed User
Longtime User

Hi @MarcoRome and thank you! But there's something I still don't understand.

I have to modify the HttpUtils2 (or OkHttpUtils) implementation and create and use this new version? In this case, where could I find the source?

But, since I tried and, after the login call, there was "something" in Job.Tag, I tried to use it as it was the "Headers". But something like:

B4X:
Dim m as Map
m = Job.Tag

led me to a runtime error.
Thank you again.
 

fbritop

Active Member
Licensed User
Longtime User
When upgrading to OKHttp:

How can we attach the InitializeAcceptAll added to the HttpJob object?. In the old times we did it directly in the httpUtils class but this is not available when using the library. I know that this option is available through the OkHttpClient object, but how do we attach it to the HttpJob?

B4X:
                Dim jobLogin As HttpJob
                jobLogin.Initialize("jobINVITATION", Me)
                Log(url)
                jobLogin.PostString(url,"")
                jobLogin.GetRequest.Timeout=8000   
                startOpen=DateTime.Now
 

Inman

Well-Known Member
Licensed User
Longtime User
Does OkHttp have any issue with saving and restoring cookies automatically? I am facing the following issue:

I have a Reddit client app which was working correctly. I hadn't updated it in over a year but due to some change in Reddit API I am forced to do so now. The app is working fine using HttpUtils and old HttpClient implementation. But when I tried to do it with okHttp (and okHttpUtils2), it does not look like it is saving the user login cookie. For example, after login to Reddit with API (which works and returns the hash and other value correctly), I immediately call the Subscribed API to get a list of subreddits the user is subscribed to. In my old app, it is still returning the correct values in JSON. But in okHttp version it returns an empty {}. I suspect this is because the API cannot detect the logged in user as the Subscribed API relies entirely on cookie to do that.

I saw a post on Stackoverflow which I think is related to this. A solution is also mentioned. Could you please take a look?

http://stackoverflow.com/questions/25461792/persistent-cookie-store-using-okhttp-2-on-android

I am using the following:
  • B4A 6.31
  • okHttp 1.01
  • OkHttpUtils2 2.20
 

ktlua

Member
Licensed User

I compile and run the program. It gives me error:
(Line:0) null
java.lang.Exception:Sub service_start signature does not matched expected public static anywheresoftware.b4a.pc.RemoveObject anywheresoftware.b4a.samples.httputils.httputilsservice_subs_0.service_start() throws java.lang.Exception
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…