B4A Library OkHttp - Replaces the Http library

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
 

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

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

 

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


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