Android Question Http OPTIONS Method

potman100

Active Member
Licensed User
Longtime User
Hi Guys

Just wondered if any one has any idea of how to send the OPTIONS method on a httpjob ?

I have searched the forum, but can't find anything on it ?

Thanks

Potman100
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Example:
B4X:
Sub Process_Globals
   Private hc As OkHttpClient
   Private out As OutputStream
End Sub

Sub Service_Create
   hc.Initialize("hc")
   Send("https://www.b4x.com")
End Sub

Public Sub Send (URL As String)
   Dim req As OkHttpRequest
   Dim builder As JavaObject
   builder.InitializeNewInstance("okhttp3.Request.Builder", Null)
   builder.RunMethodJO("url", Array(URL)).RunMethod("method", Array("OPTIONS", Null))
   Dim jo As JavaObject = req
   jo.SetField("builder", builder)
   
   hc.Execute(req, 0)
End Sub

Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
   Log("success")
   Log(Response.GetHeaders) 'ignore
   out.InitializeToBytesArray(0)
   Response.GetAsynchronously("res", out, False, 0)
End Sub

Sub res_StreamFinish (Success As Boolean, TaskId As Int)
   If Success Then
       Dim b() As Byte = out.ToBytesArray
       Log(BytesToString(b, 0, b.Length, "utf8"))
   End If
End Sub

Sub hc_ResponseError (Response As OkHttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   Log("error")
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

potman100

Active Member
Licensed User
Longtime User
Yes, I searched the forum, and as this is not a httpjob it a OkHttpRequest your suggestion does not work, the req is not initialized, and there is no method for the options request as Erel has posted above to initialize it.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Code found using google:
B4X:
HttpClient client = HttpClients.custom().build();
HttpUriRequest request = RequestBuilder.get()
  .setUri(SAMPLE_URL)
  .setHeader(HttpHeaders.CONTENT_TYPE, "application/json")
  .build();
client.execute(request);

You probably need to use javaobject to set it.
 
Upvote 0

Similar Threads

  • Question
Android Question Get Request
Replies
1
Views
660
Replies
1
Views
560
  • Question
Android Question httputlis
Replies
1
Views
1K
Replies
2
Views
759
Top