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:

DonManfred

Expert
Licensed User
Longtime User
You need to set a user agent header to set any other user agent
 

sorex

Expert
Licensed User
Longtime User
I know, Manfred.

But then I need to go back to the more native method (httpclient/response) as httpjob doesn't have this property.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

mcorbeel

Active Member
Licensed User
Longtime User
[QUOTE="If you want to use the new HttpUtils2 library you need to copy it to the internal libraries folder."[/QUOTE]

What do you mean by "internal libraries folder" ?
 

Juzer Hussain

Active Member
Licensed User
Longtime User
Hi DonManfred,

I am also facing the Permission denied issue suddenly.
I removed HTTP and HTTPUtils references.
Where shall I copy the 2 files exactly.
Along with jar files adding modules also is required or no.

Pls help.
 

sorex

Expert
Licensed User
Longtime User
@Erel,

Maybe you should rename the httputils2.jar & httputils2.xml to OkHttpUtils2.jar & OkHttpUtils2.xml ?

I guess some people get confused when they see that these files are already there and skip the overwrite.
 

scrat

Active Member
Licensed User
Longtime User
Erel,

do you plan to use okHttp 2.5 and the ConnectionSpec class ?
I think this should solve the problem described in post #46 (old cipher suite)

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Eventually I will update the library based on the new version. However you can do it now by download v2.5. Rename the jar file to okhttp-2.4.0.jar and put it in the additional libraries folder.

Based on this blog: https://github.com/square/okhttp/wiki/HTTPS it will fallback automatically. Note that it also falls back automatically in the current version so the results will most probably be the same.
 

scrat

Active Member
Licensed User
Longtime User
Yes, same problem with 2.5.0

But it's possible to create a custom connection with ConnectionSpec.
The solution is may be with "COMPATIBLE_TLS" and the good cipher suite ?
 

scrat

Active Member
Licensed User
Longtime User
yes I know , but it does not !

it's now a big problem, okhttp is not usable with many servers with old configurations and httpclient don't works with M.
What is the solution ?
 

scrat

Active Member
Licensed User
Longtime User
I just compile okhttp 2.2.0 and i can connect and download form servers with old cipher suites. strange
 
Top