Android Question okHttp

sule

Member
Licensed User
Longtime User
ok how do you implement this code with okHttp
Code:
B4X:
Sub Process_Globals
Dim hc AsHttpClient
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)
EndIf
okhttp dosnt have .SetHttpParameter property

sule
 

MarcoRome

Expert
Licensed User
Longtime User
You look HERE you will found response your question.

For next time you can use this great community in this mode:



Insert in field SEARCH your question...example okHTTP and you will be this results:

 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
there is no answer there
Sule you are very FAST. Then next time look and read better pls.
Well... in POST you have this file that you can download:


Here you have source ( Class Module ). This code you can add all function you need.

 
Upvote 0

sule

Member
Licensed User
Longtime User
Try this:
B4X:
Dim jo As JavaObject = hc
jo.GetFieldJO("client").RunMethod("setFollowRedirects", Array (False))
getting error
java.lang.RuntimeException: Object should first be initialized (JavaObject).
 
Upvote 0

sule

Member
Licensed User
Longtime User
Erel can you plase try to write simple code when auto redirecting is disabled or i just have to give up on this
when i request a url i got response from server Found 302 (temporarily moved) and i got auto redirected to another url . I just want to get Location header and stop right there ty
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code works properly:
B4X:
Sub Process_Globals
   Private hc As OkHttpClient
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     hc.Initialize("hc")
     Dim jo As JavaObject = hc
     jo.GetFieldJO("client").RunMethod("setFollowRedirects", Array (False))
   End If
   Dim req As OkHttpRequest
   req.InitializeGet("http://www.b4x.com")
   hc.Execute(req, 0)
End Sub

Sub hc_ResponseError (Response As OkHttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   Log(Response.ErrorResponse)

End Sub

Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
   
End Sub
Note that it is not possible to get the headers when there is an error. It will be fixed in the next update of OkHttp.
 
Upvote 0

sule

Member
Licensed User
Longtime User
can i get headers if i use HttpClient insted of okHttpClient
 
Upvote 0

sule

Member
Licensed User
Longtime User
Yes. I'm sure that you have already seen the code. It is the same code that uses "http.protocol.handle-redirects".
if i try to use httpclient i got thei error
Compiling generated Java code. Error
B4A line: 24
hc.Initialize(\
javac 1.8.0_112
src\b4a\example\main.java:381: error: cannot access ClientProtocolException
_hc.Initialize("hc");
^
class file for org.apache.http.client.ClientProtocolException not found
 
Upvote 0

sule

Member
Licensed User
Longtime User
the whole point is to use old httpclient becose i cant get headers with new okhttpclient
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…