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:

scrat

Active Member
Licensed User
Longtime User
Hello

I have a problem with new okHttpClient and okHttprequest.initializepost2
I try to login on a web site like this:

B4X:
aHttp.InitializeAcceptAll("Http")
Url="https://www.xxxxx/ajax/login_valid.php"
Param="login="&myLogin&"&password="&my_MD5_Pass
aReq.InitializePost2(Url,Param.GetBytes("UTF8"))
aReq.SetHeader("User-Agent","Mozilla/5.0 (Android; Linux armv7l; rv:5.0) Gecko/20110615 Firefox/5.0 Fennec/5.0")
aHttp.Execute(aReq,1000)

In Http_ResponseError I receive a 401 error

Same code with previous HttpClient works well.

The javascript code on the website

B4X:
function valid()
{
jQuery('#errcode').attr('innerHTML', "");
var encode = String(md5(jQuery('#password').val()));
var url = './ajax/login_valid.php';
var parametres = 'login=' + escape(jQuery('#login').val()) + '&password=' + encode;
jQuery.ajax(
{
url:url,
type: 'POST',
data: parametres,
success: function(transport)
{
var response = transport ;
if (response == 'ok')
{
document.location.href = "./accueil.php";
}
else
{
jQuery('#errcode').attr('innerHTML', response) ;
}
},
error: function() { alert("ajax error"); }
});
}

is there a limitation or a diffrent implementation for okHttp and post method.
Others http get requests wors well

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It works fine here. Tested with this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim hc As OkHttpClient
   hc.InitializeAcceptAll("hc")
   Dim req As OkHttpRequest
   req.InitializePost2("link", "login=user&password=md5_password".GetBytes("UTF8"))
   req.SetHeader("User-Agent","Mozilla/5.0 (Android; Linux armv7l; rv:5.0) Gecko/20110615 Firefox/5.0 Fennec/5.0")
   hc.Execute(req,1000)
End Sub


Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
   Dim out As OutputStream
   out.InitializeToBytesArray(0)
   Response.GetAsynchronously("res", out, False, TaskId)
End Sub

Sub res_StreamFinish (Success As Boolean, TaskId As Int)
   Log($" ${Success}"$)
   If Success Then Log(BytesToString(out.ToBytesArray, 0, out.ToBytesArray.Length, "utf8"))
End Sub


Sub hc_ResponseError (Response As OkHttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   Log($"Error: ${Response.ErrorResponse}, ${Reason}, ${StatusCode}"$)
   If Response <> Null Then Response.Release
End Sub
 

Devv

Active Member
Licensed User
Longtime User
I don't understand what is the difference between ok http library and httputlis 2.1 ?
 

Devv

Active Member
Licensed User
Longtime User
The OkHTTP is just the library, HTTPUtils is a tool using the OkHTTP library.
so i must only download the "httputlis2.zip" and use it , without worrying about the other attachment "okhttp.zip" right ?
 

Devv

Active Member
Licensed User
Longtime User
If you want to use HttpUtils2 v2.1 then you need to also download OkHttp as it depends on it.

OK i have to download both and but them in the internal libraries folder and include both in my project then use htpputlis as usual ?
 

scrat

Active Member
Licensed User
Longtime User
Erel,

Attached is 2 exemples : same code, first with httpclient second with okhttpclient

With Httpclient i receive the file after login, with okHttpclient first login is ok but i receive an error 401 when I download the file.
I don't understand where is my error ?
 

Attachments

  • http.zip
    6.8 KB · Views: 540
  • okhttp.zip
    6.8 KB · Views: 702

mhartwig

Member
Licensed User
Longtime User
I keep getting a noClassDefFound error regarding httpjob when trying to use this. I added the modified httputils2 library as well as the okhttp library.

Is this because I am using an older version of b4a? 2.52 to be exact.
 
Top