B4A Library OkHttp - Replaces the Http library

OkHttpUtils2 source code: [B4X] OkHttpUtils2 / iHttpUtils2 / HttpUtils2 source code
It is included as an internal library.
Usage: [B4X] OkHttpUtils2 with Wait For

The current Http library is based on the Android version of Apache HttpClient. Google has stopped updating this SDK a long time ago and they are removing it from Android M. This means that it is a good time now to switch to a different implementation.

Note that if you are using HttpUtils2 (as you should) it is also based on Http library.

The OkHttp library is based on the OkHttp SDK: http://square.github.io/okhttp/
As this is a newer SDK it supports new features such as SSL SNI, SPDY, Patch requests and better performance.

The OkHttp wrapper API is almost identical to Http library API. The main difference is that the object names start with Ok (OkHttpClient, OkHttpResponse and OkHttpRequest).

This library requires Android 2.3+.

Attached is a modified version of HttpUtils2 library (v2.12) that is based on OkHttp instead of Http.

If you want to use the new HttpUtils2 library you need to copy it to the internal libraries folder.

V1.01 - Compatible with B4J.
V1.00 - Adds support for digest authentication.

OkHttpUtils2 v2.20 is attached. It adds support for multipart requests.

Example:
B4X:
Dim j As HttpJob
j.Initialize("j", Me)
Dim fd As MultipartFileData
fd.Initialize
fd.KeyName = "file"
fd.Dir = File.DirAssets
fd.FileName = "image.png"
fd.ContentType = "image/png"
j.PostMultipart("http://...", CreateMap("param1": "value1"), Array(fd))

OkHttp and OkHttpUtils2 libraries are included in the IDE.
 
Last edited:

luke2012

Well-Known Member
Licensed User
Longtime User
yes.

Ideally the webservices does return only a json-string with the result answer(s)

B4X:
    job.PostString("http://www.targetsite.org/webservice.php", "firstname=John&lastname=Doe")

The above (web service call) method could be considered cross-platform (Java / PHP / .Net) ?
 

DonManfred

Expert
Licensed User
Longtime User
i dont know how it would be in .net or even in java.

But is is the right way and it should be cross-platform... At the end it depends on the serversoftware you are running...
 

johnaaronrose

Active Member
Licensed User
Longtime User
Converting to OkHttp, on compiling I get:
B4A version: 5.80
Parsing code. Error
Error parsing program.
Error description: HttpUtils2Service is declared twice. You should either remove the library reference or the code module.

HttpUtils2Service is a Service module. It has no Library entry, as would be shown in the Libraries Manager. Any ideas for the message?
 

Attachments

  • JewishWorld.zip
    43 KB · Views: 450

johnaaronrose

Active Member
Licensed User
Longtime User
@DonManfred When I click the Libraries Manager tab, neither httpjob nor httputilsservice is shown in the list. OkHttp & OkHttpUtils are shown with ticks against them. There are entries for HTTP & HttpUtils2, but neither of them have ticks: should I remove those entries (if so, how)? I have HttpJob as a Class module and HttpUtils2Service as a Service module in my project. I have entries of OkHttp & OkHttpUtils2 in the Libraries Manager list with ticks against them.
 

johnaaronrose

Active Member
Licensed User
Longtime User
@manfred I switched to using OkHttp etc as Erel said (in first post on this thread):
"The current Http library is based on the Android version of Apache HttpClient. Google has stopped updating this SDK a long time ago and they are removing it from Android M. This means that it is a good time now to switch to a different implementation.
Note that if you are using HttpUtils2 (as you should) it is also based on Http library.
The OkHttp library is based on the OkHttp SDK: http://square.github.io/okhttp/"
and because my app no longer worked: I haven't used it for at least a year and I haven't updated it for approx 3 years.

I am using the standard HttpJob Class module. Should it be in Additional Libraries or somewhere else rather than me copying the source? As you say, I have customized the original HttpUtils2Service service module's source as I have coding in to display a progress bar.

The Libraries Manager tab's list has entries for HTTP & HttpUtils2, but neither of them have ticks: should I remove those entries (if so, how)?
 

DonManfred

Expert
Licensed User
Longtime User
I have customized the original HttpUtils2Service service module's source as I have coding in to display a progress bar.
Yes, i noticed.
Using modified version you should make sure to UPADATE the modules to have the newest ones. Download the newest okhttputils source modules (See Post #1 in this Thread). AND do the needed changes for your progress here TOO.
Use the "new" two modules then...
 

DonManfred

Expert
Licensed User
Longtime User
Please start a NEW thread in the questions forum for any further questions...
Upload the updated project in the new thread if you still dont get it to work.
Also post the link to the library you are using... I get a message of a missing library (i dont have this lib)
 

johnaaronrose

Active Member
Licensed User
Longtime User
Yes, i noticed.
Using modified version you should make sure to UPADATE the modules to have the newest ones. Download the newest okhttputils source modules (See Post #1 in this Thread). AND do the needed changes for your progress here TOO.
Use the "new" two modules then...
I'm confused. Erel says in his first post on this thread "OkHttp and OkHttpUtils2 libraries are included in the IDE.". So as I haven't downloaded them either as .zip (let alone installed these) or as Additional Library entries (which would presumably be have .jar & .xml files), but they are in the Library Manager's tab list with ticks, so therefore they are recent versions as I'm using B4A 5.80.
 

johnaaronrose

Active Member
Licensed User
Longtime User
Please start a NEW thread in the questions forum for any further questions...
Upload the updated project in the new thread if you still dont get it to work.
Also post the link to the library you are using... I get a message of a missing library (i dont have this lib)
What do you suggest as a title for a NEW Thread?
 

johnaaronrose

Active Member
Licensed User
Longtime User
Yes, they are in the library tab.
BUT you are using a customized version of the old library...
Download the newest source (see post #1) and do the changes you made in the old one again in the new sources... Use the new modules then.
I don't understand what you mean by a customized version of the old library. As I said before, I have ticks against OkHttp & OkHttpUtils2 (but no ticks against HTTP & HttpUtils2) in the Library Manager tab's list. So is that correct?
I have a modules for HttpJob & HttpUtils2Service. Am I correct in stating that I need to take the new versions of these from Erel's post #1 and amend them in the same way as I previously amended them?
 

DonManfred

Expert
Licensed User
Longtime User
I have a modules for HttpJob & HttpUtils2Service. Am I correct in stating that I need to take the new versions of these from Erel's post #1 and amend them in the same way as I previously amended them?
Correct. You do not need to mark the library in the libary tab (okhttputils2) as you are using the source of it (the two modules).

Any further question; Please create a new thread in the questions forum. I´ll not give any more answers here.
 

OliverA

Expert
Licensed User
Longtime User

Jmu5667

Well-Known Member
Licensed User
Longtime User
I have spent several hours the last two days trying to make a SAML authentication work. The exacts same code ported to PHP using cUrl worked fine, but not in B4A.

After much debugging and analyzing with mitmproxy I finally realized that OkHttpUtils2 PostString does not automatically encode the POST parameters. Instead, I had to use StringUtils.EncodeUrl:

B4X:
Dim SU As StringUtils
saml = SU.EncodeUrl(saml, "UTF-8")
j.PostString(url, "SAMLResponse=" & saml)

Then it worked.

After I found the cause of the problems, I realized that I could also use PostMultipart which does not need url encoding:

B4X:
Dim m As Map
m.Initialize
m.Put("SAMLResponse", saml)
j.PostMultipart(url, m, Null)

As far as I know, HttpUtils2 does not need encoding of the POST parameters.

Hopefully, this forum post can save others from wasting a few hours :)


Hi Martin

I would be interested in know more about SAML as it has been requested by one of our clients. As I yet I have no knowledge. Can you suggest any info to read and maybe an example of the url used in the sample code.

Thanks in advance

John.
 
Top