1.07
anywheresoftware.b4h.okhttp.OkHttpClientWrapper
OkHttpClient
OkHttpClient allows you to make Http requests. Instead of using OkHttpClient directly it is recommended to use <link>OkHttpUtil2|https://www.b4x.com/android/forum/threads/b4x-okhttputils2-with-wait-for.79345/</link>
modules which are much simpler to use.
process
ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
ResponseError (Response As OkHttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
android.permission.INTERNET
Initialize
Initializes this object.
IMPORTANT: this object should be declared in Sub Process_Globals.
EventName - The prefix that will be used for ResponseSuccess and ResponseError events.
void
EventName
java.lang.String
ExecuteCredentials
Same behavior as Execute. The UserName and Password will be used for Basic authentication and Digest authentication.
boolean
ba
anywheresoftware.b4a.BA
Request
anywheresoftware.b4h.okhttp.OkHttpClientWrapper.OkHttpRequest
TaskId
int
UserName
java.lang.String
Password
java.lang.String
Execute
Executes the OkHttpRequest asynchronously. ResponseSuccess or ResponseError events will be fired later.
If there is a request with the same TaskId already running then this method will return False and the new request will not be submitted.
boolean
ba
anywheresoftware.b4a.BA
HttpRequest
anywheresoftware.b4h.okhttp.OkHttpClientWrapper.OkHttpRequest
TaskId
int
InitializeAcceptAll
Similar to Initialize, with one important difference. All SSL certificates will be automatically accepted.
<b>This method should only be used when trying to connect to a server located in a secured network</b>.
void
EventName
java.lang.String
IsInitialized
boolean
anywheresoftware.b4h.okhttp.OkHttpClientWrapper.OkHttpRequest
OkHttpRequest
process
InitializePatch
Initializes the request and sets it to be a Http Patch method.
The specified InputStream will be read and added to the request.
void
URL
java.lang.String
InputStream
java.io.InputStream
Length
int
InitializePut
Initializes the request and sets it to be a Http Put method.
The specified InputStream will be read and added to the request.
void
URL
java.lang.String
InputStream
java.io.InputStream
Length
int
InitializePost
Initializes the request and sets it to be a Http Post method.
The specified InputStream will be read and added to the request.
void
URL
java.lang.String
InputStream
java.io.InputStream
Length
int
RemoveHeaders
Removes all headers with the given name.
void
Name
java.lang.String
InitializeDelete
Initializes the request and sets it to be a Http Delete method.
void
URL
java.lang.String
SetContentType
Sets the Mime header of the request.
This method should only be used with requests that have a payload.
void
ContentType
java.lang.String
InitializePatch2
Initializes the request and sets it to be a Http Patch method.
The specified Data array will be added to the request.
Unlike InitializePost this method will enable the request to retry and send the data several times in case of IO errors.
void
URL
java.lang.String
Data
byte[]
InitializePut2
Initializes the request and sets it to be a Http Put method.
The specified Data array will be added to the request.
Unlike InitializePost this method will enable the request to retry and send the data several times in case of IO errors.
void
URL
java.lang.String
Data
byte[]
InitializeHead
Initializes the request and sets it to be a Http Head method.
void
URL
java.lang.String
SetContentEncoding
Sets the encoding header of the request.
void
Encoding
java.lang.String
InitializeGet
Initializes the request and sets it to be a Http Get method.
void
URL
java.lang.String
SetHeader
Sets the value of the header with the given name. If no such header exists then a new header will be added.
void
Name
java.lang.String
Value
java.lang.String
InitializeDelete2
Initializes the request and sets it to be a Http Delete method with the given payload.
void
URL
java.lang.String
Data
byte[]
InitializePost2
Initializes the request and sets it to be a Http Post method.
The specified Data array will be added to the request.
Unlike InitializePost this method will enable the request to retry and send the data several times in case of IO errors.
void
URL
java.lang.String
Data
byte[]
Timeout
int
t
int
Gets or sets the request timeout, measured in milliseconds. Default value is 30,000 (30 seconds).
anywheresoftware.b4h.okhttp.OkHttpClientWrapper.OkHttpResponse
OkHttpResponse
An object that holds the response returned from the server.
The object is passed in the ResponseSuccess event.
You can choose to read the response synchronously or asynchronously.
It is important to release this object when it is not used anymore by calling Release.
process
StreamFinish (Success As Boolean, TaskId As Int)
Release
Frees resources allocated for this object.
void
GetAsynchronously
Asynchronously reads the response and writes it to the given OutputStream.
If there is a request with the same TaskId already running then this method will return False, and the response object will be released.
The StreamFinish event will be raised after the response has been fully read.
EventName - The sub that will handle the StreamFinish event.
Output - The stream from the server will be written to this stream.
CloseOutput - Whether to close the specified output stream when done.
TaskId - The task id given to this task.
Example:<code>
Sub Http_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
Response.GetAsynchronously("ImageResponse", _
File.OpenOutput(File.DirInternalCache, "image.jpg", False), True, TaskId)
End Sub
Sub ImageResponse_StreamFinish (Success As Boolean, TaskId As Int)
If Success = False Then
Msgbox(LastException.Message, "Error")
Return
End If
ImageView1.Bitmap = LoadBitmap(File.DirInternalCache, "image.jpg")
End Sub</code>
boolean
ba
anywheresoftware.b4a.BA
EventName
java.lang.String
Output
java.io.OutputStream
CloseOutput
boolean
TaskId
int
GetHeaders
Returns a Map object with the response headers.
Each elements is made of a key which is the header name and a value which is a list containing the values (one or more).
Example:<code>
Dim list1 As List
list1 = response.GetHeaders.Get("Set-Cookie")
For i = 0 To list1.Size - 1
Log(list1.Get(i))
Next</code>
anywheresoftware.b4a.objects.collections.Map
StatusCode
int
Returns the response Http code.
Returns -1 is the status code is not available.
ContentLength
long
Returns the response body length.
ErrorResponse
java.lang.String
Returns the server response as a string (for failed responses only).
ContentEncoding
java.lang.String
Returns the Content-Encoding header.
ContentType
java.lang.String
Returns the Content-Type header.
1.22
okhttp-3.5.0
okio-1.11.0