Android Question method PROPFIND with XML-Data for OKHTTPUtils2, please help...

pesfronau

New Member
Licensed User
Longtime User
I modified a function, which Erel learned us in this thread, to put it in then HTTPUtils.bas source of OKHttpUtils2.

This is the function to insert in HTTPUtils:
B4X:
'Submits a HTTP METHOD -X request.
'pMethod is like "PROPFIND","MKCOL","PROPPATCH","COPY","MOVE","REPORT" - needed eg for WEBDAV
'headers = map of headerkeys headervalues
Public Sub SendMethod(pMethod As String, Link As String,headers As Map)
    Dim builder As JavaObject
    builder.InitializeNewInstance("okhttp3.Request.Builder",Null)
    'builder.RunMethodJO("addHeader", Array(Link))
    builder.RunMethodJO("url", Array(Link))
    builder.RunMethod("method", Array(pMethod, Null))
    Dim jo As JavaObject = req
    jo.SetField("builder", builder)
    If headers <> Null  And headers.IsInitialized Then
       For Each key As String In headers.Keys
         Select True
            Case key.ToLowerCase.Contains("content-type")
                req.SetContentType(headers.Get(key))
            Case Else
                req.SetHeader(key,headers.Get(key))
         End Select
       Next
    end if
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub

Using the new OKHTTPUtils2 i can call many WEBDAV API's for Nextcloud or Owncloud, because this API's need no POST, PUT, GET or any other implemented Methods from OKHTTPUtils, they need "PROPFIND","PROPPATCH" etc.

!!!But some API's also need to transfer XML-Data with method "PROPFIND", and this is not implemented!!!

So i modified the function again, to use the second Parameter in the method-function which in Erel's original Function is NULL see this:
.RunMethod("method", Array("OPTIONS", Null))
Exactly this second Parameter is normaly a okhttp3.RequestBody which holds the Data.
This is explained here.

Does anyone have an idea how to create and pass a okhttp3.RequestBody filled with Data to the second Parameter of the Request.Builder method(String method, RequestBody body)?

I tried it like this in my function which i inserted in httpjob but i get an error.

My Code:
B4X:
'Submits a HTTP METHOD -X request.
'Like PROPFIND","MKCOL","PROPPATCH","COPY","MOVE","REPORT" - needed eg for WEBDAV
'headers = map of headerkeys headervalues
'Data() is RequestBody Data in bytes() of MediaType as string eg "text/xml;charset=utf-8"
Public Sub SendMethodWithData(sMethod As String, Link As String,sMediatype As String, Data() As Byte,headers As Map)

    Dim RequestBody As JavaObject
    RequestBody.InitializeNewInstance("okhttp3.RequestBody",Null)
    
    Dim Mediatype As JavaObject
    Mediatype.InitializeNewInstance("okhttp3.MediaType",Null)
    
    Dim builder As JavaObject
    builder.InitializeNewInstance("okhttp3.Request.Builder",Null)
    builder.RunMethodJO("url", Array(Link))
    builder.RunMethod("method", Array(sMethod, RequestBody.RunMethodJO("create", Array(Mediatype.RunMethodJO("parse",Array("text/xml; charset=utf-8")),Data))))
    Dim jo As JavaObject = req
    jo.SetField("builder", builder)
    'req.InitializePut2(Link,Data) ' does not function because method is back to PUT
    If headers <> Null  And headers.IsInitialized Then
       For Each key As String In headers.Keys
         Select True
            Case key.ToLowerCase.Contains("content-type")
                req.SetContentType(headers.Get(key))
            Case Else
                req.SetHeader(key,headers.Get(key))
         End Select
       Next
    end if
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub

This function throws the following error in line RequestBody.InitializeNewInstance("okhttp3.RequestBody",Null):
java.lang.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)

I need this possibility for creating a WEBDAV Library for Nextcloud.

Some help would be nice.

Danke
Peter
 

pesfronau

New Member
Licensed User
Longtime User
Yes, i found the solution myself.
The correct function is :
B4X:
'Submits a HTTP METHOD -X request.
'Like PROPFIND","MKCOL","PROPPATCH","COPY","MOVE","REPORT" - needed eg for WEBDAV
'headers = map of headerkeys headervalues
'Data() is RequestBody Data in bytes() of MediaType as string eg "text/xml;charset=utf-8"
Public Sub SendMethodWithData(sMethod As String, Link As String,sMediatype As String, Data() As Byte,headers As Map)
    Dim Mediatype As JavaObject
    Dim RequestBody As JavaObject
    Mediatype.InitializeStatic("okhttp3.MediaType")
    RequestBody.InitializeStatic("okhttp3.RequestBody")
    Dim builder As JavaObject
    builder.InitializeNewInstance("okhttp3.Request.Builder",Null)
    builder.RunMethodJO("url", Array(Link))
    'builder.RunMethod("method", Array(pMethod, Null))
    builder.RunMethod("method", Array(sMethod,RequestBody.RunMethodJO("create", Array(Mediatype.RunMethodJO("parse",Array(sMediatype)),Data))))
    Dim jo As JavaObject = req
    jo.SetField("builder", builder)
    'req.InitializePut2(Link,Data)
    If headers <> Null  And headers.IsInitialized Then
       For Each key As String In headers.Keys
         Select True
            Case key.ToLowerCase.Contains("content-type")
                req.SetContentType(headers.Get(key))
            Case Else
                req.SetHeader(key,headers.Get(key))
         End Select
       Next
    end if
    CallSubDelayed2(CHIHttpUtils2Service, "SubmitJob", Me)
End Sub

The failure was the wrong initialization Method of the okhttp3.RequestBody and the okhttp3.MediaType.
Now feel free to implement this two functions in your own OKHttpUtils2.

Gruß
Peter
 
Upvote 0
Top