Android Question Is this best way to Create Directory on Koofr Cloud Storage

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I am writting some routines to allow me to work with Koofr cloud storage

Asked ChatGPT to help me and we worked out this code.
Create Directory on Koofr:
Sub CreateKoofrDirectory(DirUrl As String) As ResumableSub
    Dim joClient As JavaObject
    joClient.InitializeNewInstance("okhttp3.OkHttpClient", Null)
    
    ' Build request
    Dim joBuilder As JavaObject
    joBuilder.InitializeNewInstance("okhttp3.Request$Builder", Null)
    joBuilder.RunMethod("url", Array(DirUrl))
    
    ' Empty body
    Dim joRequestBody As JavaObject
    joRequestBody = joRequestBody.InitializeStatic("okhttp3.RequestBody").RunMethod("create", Array(Null, ""))
    
    ' Set MKCOL method
    joBuilder.RunMethod("method", Array("MKCOL", joRequestBody))
    
    ' Add global Authorization header
    Dim Auth As String = "Basic " & authBase64
        
    joBuilder.RunMethod("addHeader", Array("Authorization", Auth))
    
    ' Build request
    Dim joRequest As JavaObject = joBuilder.RunMethod("build", Null)
    
    ' Execute synchronously (run in background thread!)
    Dim joCall As JavaObject = joClient.RunMethod("newCall", Array(joRequest))
    Dim joResponse As JavaObject = joCall.RunMethod("execute", Null)
    
    Dim code As Int = joResponse.RunMethod("code", Null)
    joResponse.RunMethod("close", Null)
    
    Log($"Response Code to Create Directory ${code}"$)
    Return code
End Sub

The code works Perfect. Just wondering if this is the proper way to do this.

I must have gone through 100 different ways with Chat before we got this one to work
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I later asked about the code note being Async and it gave me this
Create Directory on Koofr Async:
Sub CreateKoofrDirectoryAsync(DirUrl As String) As ResumableSub
    Dim code As Int
    Dim joClient As JavaObject
    joClient.InitializeNewInstance("okhttp3.OkHttpClient", Null)
   
    ' Build request
    Dim joBuilder As JavaObject
    joBuilder.InitializeNewInstance("okhttp3.Request$Builder", Null)
    joBuilder.RunMethod("url", Array(DirUrl))
   
    ' Empty body
    Dim joRequestBody As JavaObject
    joRequestBody = joRequestBody.InitializeStatic("okhttp3.RequestBody").RunMethod("create", Array(Null, ""))
   
    ' Set MKCOL method
    joBuilder.RunMethod("method", Array("MKCOL", joRequestBody))
   
    ' Add global Authorization header
    Dim Auth As String = "Basic " & authBase64
       
    joBuilder.RunMethod("addHeader", Array("Authorization", Auth))
   
    ' Build request
    Dim joRequest As JavaObject = joBuilder.RunMethod("build", Null)
   
    ' Execute in background thread
    Dim joCall As JavaObject = joClient.RunMethod("newCall", Array(joRequest))
    Dim joResponse As JavaObject
   
    ' Wrap the blocking execute in Wait For / Resume
    CallSubDelayed2(Me, "RunInBackground", joCall)
    Wait For RunInBackground_Result(res As JavaObject)
    joResponse = res
   
    code = joResponse.RunMethod("code", Null)
    joResponse.RunMethod("close", Null)
   
    Return code
End Sub

' Helper for background execution
Sub RunInBackground(joCall As JavaObject)
    Dim joResponse As JavaObject
    joResponse = joCall.RunMethod("execute", Null)
    CallSubDelayed2(Me, "RunInBackground_Result", joResponse)
End Sub


Thanks for any help / ideas in advance
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I wouldn't use the code you posted. Better to stick with OkHttpUtils2.

You can change the HTTP method to MKCOL with:
B4X:
Dim j As HttpJob
j.Initialize("", Me)
j.Download(url)
j.GetRequest.As(JavaObject).GetFieldJO("builder").RunMethod("method", Array("MKCOL", Null))
'or with an empty body (I don't think that it is needed but might be wrong).
'Dim joRequestBody As JavaObject
 'joRequestBody = joRequestBody.InitializeStatic("okhttp3.RequestBody").RunMethod("create", Array(Null, ""))
'j.GetRequest.As(JavaObject).GetFieldJO("builder").RunMethod("method", Array("MKCOL", joRequestBody))
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
    Log(j.GetString)
Else
    Log(j.ErrorMessage)
End If
j.Release
 
Upvote 1

Robert Valentino

Well-Known Member
Licensed User
Longtime User
WOW, thanks Erel that is surely a lot cleaner.

Just needed to add 1 line to set the Authorization and works perfectly

j.GetRequest.SetHeader("Authorization", BasicAuthorized)
 
Upvote 0
Top