B4A Library OkHttp - Replaces the Http library

Erel

Administrator
Staff member
Licensed User
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:

thedesolatesoul

Expert
Licensed User
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.
Do you know why? Are they trying to merge it into GooglePlayServices? Or are they moving towards Volley?
 

incendio

Well-Known Member
Licensed User
What about RDC? As I know, RDC use HttpUtils2 library, is this new library works OK with RDC?
 

tchart

Well-Known Member
Licensed User
On a slightly related topic, would this library have a synchronous http method?
 

tchart

Well-Known Member
Licensed User
It is not possible (for good reasons) to use synchronous requests in Android. Such requests can easily freeze the app for a long time and Android will kill the process after 5 seconds.
Thanks Erel, Ive brought this up before and understand the issue on Android. I was actually hoping for a solution I could use on B4J. I have a requirement for a synchronous web request.

Ive started wrapping https://github.com/kevinsawicki/http-request which works nicely and supports synchronous requests.

Thanks
 

itgirl

Active Member
Licensed User
"they are removing it from Android" so newer Androids wont work with our old precious HttpUtils2:confused: so should we move on to the OkHttp and update all apps or stick with the old method ?! since it's a beta :confused:
 

lemonisdead

Well-Known Member
Licensed User
Dear Erel : I probably did something wrong as I am getting some error in the log window

B4X:
** Activity (main) Create, isFirst = true **
java.lang.NoClassDefFoundError: java.net.IDN
    at com.squareup.okhttp.HttpUrl$Builder.domainToAscii(HttpUrl.java:1315)
    at com.squareup.okhttp.HttpUrl$Builder.canonicalizeHost(HttpUrl.java:1195)
    at com.squareup.okhttp.HttpUrl$Builder.parse(HttpUrl.java:959)
    at com.squareup.okhttp.HttpUrl.parse(HttpUrl.java:585)
    at com.squareup.okhttp.Request$Builder.url(Request.java:156)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest.InitializeGet(OkHttpClientWrapper.java:257)
    at anywheresoftware.b4a.samples.httputils2.httpjob._v7(httpjob.java:73)
    at anywheresoftware.b4a.samples.httputils2.main._activity_create(main.java:328)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at anywheresoftware.b4a.samples.httputils2.main.afterFirstLayout(main.java:100)
    at anywheresoftware.b4a.samples.httputils2.main.access$100(main.java:17)
    at anywheresoftware.b4a.samples.httputils2.main$WaitForLayout.run(main.java:78)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)

What I did is : copy the OkHttp Library and the HttpUtils2 XML and Jar in my libraries folder. Copied the HttpJob shared to my shared modules folder.
I tried to compile and install on Nexus 6 (Lollipop), Nexus 4 (not upgraded KitKat) and Moto G XT 1032 (Lollipop)
I did not modify the HttpUtils2 example.
 
Last edited:

lemonisdead

Well-Known Member
Licensed User
But you REPLACED your old httputils2 with the one here provided?
Hello Manfred,
I did in my Additional Libraries folder but I have missed to remove the ones in the C:\Basic4android\5\Libraries. It works now. On my system, strangely (or perhaps not), the Additional Libraries folder is not selected against the standard folder.
I am sorry for the disturb : it works now
 
Top