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:

Bryanne Vega

Member
Licensed User
Longtime User
Does this error only happen on this specific device?
Does it happen with all SSL addresses that you try?

Yes a specific POS Device running their own branded ROM etc etc.
Have not tried others since it's their own HTTPs services.


Maybe if I use :443 port?
 

Blue.Sky

Active Member
Licensed User
Longtime User
I have a problem with below error
java.lang.NoClassDefFoundError: anywheresoftware.b4a.objects.ServiceHelper$StarterHelper
What is problem?
 

rscheel

Well-Known Member
Licensed User
Longtime User
If you used this library OkHttpUtils2.zip should not have problems with Android 6.0.1?

Replace HttpUtils2 by OkHttpUtils2 and I have not had any problem in Android 5.1.1, although I have not had the opportunity to test in 6.0.1.
 

DonManfred

Expert
Licensed User
Longtime User

MarcoRome

Expert
Licensed User
Longtime User
Hi all. Time ago i write time ago one library Paypal credit card, now seem that this library have a few problems with SSL, when i call:
B4X:
https://api.sandbox.paypal.com/v1/oauth2/token","grant_type=client_credentials")

find message i have this error:
javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
After i change this line code:
B4X:
Change hc.Initialize in HttpUtils2Service to hc.InitializeAcceptAll
as suggestion Erel HERE

Now i have this error:
javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException:
SSL handshake aborted: ssl=0x7a11ad08: Failure in SSL library, usually a protocol error
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:741 0x74686718:0x00000000)

Any idea ?
Thank you
Marco
 

sorex

Expert
Licensed User
Longtime User
is the user agent forced/fixed to okhttp/2.4.0 ?

I can't seem to tweak it with the code below.

B4X:
dlj.Download(url)
dlj.GetRequest.SetHeader("User_Agent", "Mozilla/5.0 (Android; Linux armv7l; rv:5.0) Gecko/20110615 Firefox/5.0 Fennec/5.0")
 

DonManfred

Expert
Licensed User
Longtime User
dlj.GetRequest.SetHeader("User_Agent", "Mozilla/5.0 (Android; Linux armv7l; rv:5.0) Gecko/20110615 Firefox/5.0 Fennec/5.0")
have you tried
B4X:
dlj.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Android; Linux armv7l; rv:5.0) Gecko/20110615 Firefox/5.0 Fennec/5.0")
- instead of _
 

sorex

Expert
Licensed User
Longtime User
indeed, I just noticed it. strange because php lists it with an underscore. anyway, it works now.
 

DonManfred

Expert
Licensed User
Longtime User
https://tools.ietf.org/html/rfc7231
5.5. Request Context

The following request header fields provide additional information
about the request context, including information about the user, user
agent, and resource behind the request.

+-------------------+---------------+
| Header Field Name | Defined in... |
+-------------------+---------------+
| From | Section 5.5.1 |
| Referer | Section 5.5.2 |
| User-Agent | Section 5.5.3 |
+-------------------+---------------+
 

Bel

Member
Licensed User
:(:(:(
I cannot use okhttputil
I used picasso and com.jeremyfeinstein.slidingmenu in project
I didn't have a problem in httputill2 but now i need to use okhttputill it get below error
Please help :(
java.lang.NoClassDefFoundError: com.squareup.okhttp.HttpResponseCache
 

Bel

Member
Licensed User
Can you post the full error message from the logs?
yes
Below error :
** Activity (main) Pause, UserClosed = false **
** Activity (actmenu) Create, isFirst = true **
actmenu_activity_create (java line: 468)
java.lang.NoClassDefFoundError: com.squareup.okhttp.HttpResponseCache
at com.squareup.picasso_OkHttpLoader.<init>(OkHttpLoader.java:58)
at com.squareup.picasso_OkHttpLoader.<init>(OkHttpLoader.java:35)
at com.squareup.picasso_OkHttpLoader.<init>(OkHttpLoader.java:25)
at com.squareup.picasso.Utils$OkHttpLoaderCreator.create(Utils.java:287)
at com.squareup.picasso.Utils.createDefaultLoader(Utils.java:199)
at com.squareup.picasso.Picasso$Builder.build(Picasso.java:617)
at com.squareup.picasso.Picasso.with(Picasso.java:542)
at uk.co.martinpearman.b4a.squareup.picasso.Picasso.Initialize(Picasso.java:51)
at shop.opencart.appstudent.actmenu._activity_create(actmenu.java:468)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at shop.opencart.appstudent.actmenu.afterFirstLayout(actmenu.java:102)
at shop.opencart.appstudent.actmenu.access$000(actmenu.java:17)
at shop.opencart.appstudent.actmenu$WaitForLayout.run(actmenu.java:80)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5392)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
** Activity (actmenu) Resume **
slidemenu_addheader (java line: 73)
java.lang.NullPointerException
at shop.opencart.appstudent.slidemenu._addheader(slidemenu.java:73)
at shop.opencart.appstudent.actmenu._activity_resume(actmenu.java:638)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at shop.opencart.appstudent.actmenu.afterFirstLayout(actmenu.java:108)
at shop.opencart.appstudent.actmenu.access$000(actmenu.java:17)
at shop.opencart.appstudent.actmenu$WaitForLayout.run(actmenu.java:80)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5392)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
** Service (pushservice) Start **
slidemenu_addheader (java line: 73)
java.lang.NullPointerException
at shop.opencart.appstudent.slidemenu._addheader(slidemenu.java:73)
at shop.opencart.appstudent.actmenu._wishlist_receive(actmenu.java:1713)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.keywords.Common$5.run(Common.java:996)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5392)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
 

Bel

Member
Licensed User
There are several other errors here. Try it in a new project with only OkHttpUtils2 checked.
I dont have problem in new project.it's working good
But in current project have error
How solve it?
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
I'm getting errors once I switched httputils2 to this

java.io.FileNotFoundException: /data/user/0/com.omnicorp.lcarui.test/cache/3: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:209)
at com.omnicorp.lcarui.test.httpjob._getstring2(httpjob.java:213)
at com.omnicorp.lcarui.test.httpjob._getstring(httpjob.java:202)
at com.omnicorp.lcarui.test.main._jobdone(main.java:11138)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:897)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:839)
at com.omnicorp.lcarui.test.stimer._jobdone(stimer.java:339)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.keywords.Common$5.run(Common.java:996)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
... 20 more
java.io.FileNotFoundException: /data/user/0/com.omnicorp.lcarui.test/cache/3: open failed: ENOENT (No such file or directory)
 
Top