B4A Library OkHttp - Replaces the Http library

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: 631
  • okhttp.zip
    6.8 KB · Views: 804

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.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…