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:

sule

Member
Licensed User
Longtime User
ok how do you implement this code with okHttp
B4X:
Sub Process_Globals
    Dim hc As HttpClient  
End Sub

Sub Globals
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        hc.Initialize("hc")
        hc.SetHttpParameter("http.protocol.handle-redirects", False)
    End If
okhttp dosnt have .SetHttpParameter property
 

DonManfred

Expert
Licensed User
Longtime User
Please create a new thread in the questions forum for this question
 

ELCHARO

Member
Licensed User
Longtime User
My app work use httpjobs and work well in samsung s7, but fail y others mobil and emulator.
because the peer close the connection.

Use android-23
okhttp library
include in project httpjob and httutils2service files with hc.InitializeAcceptAll("hc") added.

Any idea why not work in all androids.

Thanks.
 

MarcoRome

Expert
Licensed User
Longtime User
My app work use httpjobs and work well in samsung s7, but fail y others mobil and emulator.
because the peer close the connection.

Use android-23
okhttp library
include in project httpjob and httutils2service files with hc.InitializeAcceptAll("hc") added.

Any idea why not work in all androids.

Thanks.

More details pls.
You have the code to show ?
The window Log which displays error ?
 
Last edited:

Douglas Farias

Expert
Licensed User
Longtime User
hi, @Erel
its possible send info via header in this code of your example?
i need upload a file and send a token via http header at same time.
it is possible? upload file + SetHeader

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))


thx
 

MarcoRome

Expert
Licensed User
Longtime User
hi, @Erel
its possible send info via header in this code of your example?
i need upload a file and send a token via http header at same time.
it is possible? upload file + SetHeader

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))

thx

upload_2017-1-24_15-48-27.png
 

Ohanian

Active Member
Licensed User
Longtime User
Hi,
is it possible to update the lib to the latest okhttp version?
it has many improments and some new methods like Cancel.
tnx
 

Marco Nissen

Active Member
Licensed User
Longtime User
It will be updated with the next version of B4A.

Hi Erel

I have now updated my largest project to OKHTTP.

Now there's one problem - the OAUTH library still uses the old format - any idea how can use OAUTH (1.0) with OkHTTP?

THanks
Marco
 

Marco Nissen

Active Member
Licensed User
Longtime User
It is not possible. The OAuth library will only work with Http library.

What are you using it for?


I wanted to use B4AGridView, and that library requires because to upgrade to API25.
API25 does not work without OKHTTP, so I have to upgrade to that.
The app connects to the API of Ravelry, which is OAUTH 1.0

Do you have any alternatives for this library?

Here are some implementations for okhttp that are reported to work:
https://github.com/square/okhttp/wiki/Works-with-OkHttp

https://github.com/rburgst/okhttp-digest
https://github.com/pakerfeldt/okhttp-signpost

Or can you possibly quickly show how I can use these libraries by myself?

Thank you
Marco
 

Marco Nissen

Active Member
Licensed User
Longtime User
Note that digest authentication is already supported.

It will be possible to integrate the signpost library when the next version of B4A is released.


Ok, what do you recommend?
a. I switch back to http from okhttp
b. I try to implement somehow request signage myself
c. I wait until signing is integrate in the next B4A version

For a: is that recommended ? I understood I should go away from it
For b: any recommendations of similar integrations, so I can start in an educated way?
For c: how long would it take for the next update of B4A?

Thanks -- and thousand thanks for your effort!
Marco
 

Marco Nissen

Active Member
Licensed User
Longtime User
I recommend you to use Http for now (assuming that the web service doesn't support OAuth 2).

Note that it will not be supported immediately with the next version of B4A. It will be possible to then integrate the open source project as the relevant field will be exposed.


The problem is, if I compile with API 24 or API 25, then I get this error when I don't convert to OkHttp, and you stated somewhere else to switch to OkHttp to fix the issue

B4X:
B4A version: 6.50
Parsing code.    (0.75s)
Compiling code.    (1.77s)
Compiling layouts code.    (0.21s)
Organizing libraries.    (6.99s)
Generating R file.    (2.46s)
Compiling generated Java code.    Error
B4A line: 115
hc.Initialize(\
javac 1.8.0_05
src\com\wurzelkraut\Stash2Go\addphoto.java:490: error: cannot access ClientProtocolException
_hc.Initialize("hc");
              ^
  class file for org.apache.http.client.ClientProtocolException not found

I use this code
B4X:
Dim hc As HttpClient
Dim req As HttpRequest
hc.Initialize("hc")   ' throws the above issue
...
req = CreatePostRequest(url, NV, files) ' this constructs a multi part post request
ah.signRequest2(req)  ' this is where I use Oauth 
hc.Execute(req, 1)

So what should I do ? Stay with API23 and not being able to use B4AGridView ?
 

DonManfred

Expert
Licensed User
Longtime User
The problem is, if I compile with API 24 or API 25
Starting from android 6 the http lib does not work anymore!

If you need to stay on this lib than you need to compile your app against api 22 or lower.

It will not work with Api 23 or above.

Maybe the web api you are using support OAuth2 too.
Which web api are you using?

Probably it is better to create a new thread in the questions forum for your issue

Edit:
If it is http://www.ravelry.com API which you are using.
Ravelry does support digest authentification additional to OAuth 1.
You should be able to use okhttputils using digest authentification to access this api.
 
Last edited:
Top