Skip to content

Instantly share code, notes, and snippets.

@maq796113
Created January 8, 2023 14:46
Show Gist options
  • Save maq796113/06332ade664b2ab7dd872e42c3ff2cf3 to your computer and use it in GitHub Desktop.
Save maq796113/06332ade664b2ab7dd872e42c3ff2cf3 to your computer and use it in GitHub Desktop.
I am trying to access my smb server running on my azure vm. I am uploading the file there from the android mobile app. Ignore the MainActivity.kt, I have shared a snippet of the code which is relevant to the issue.
package com.example.qurantutor.network
object BaseURL {
const val smbbaseUrl = "smb://$publicIP/shared/"
}
private fun uploadRecitation() {
val requestFile = RequestBody.create(MediaType.parse("audio/x-wav"), file)
val body = MultipartBody.Part.createFormData("file", file.name, requestFile)
val call = smbService.uploadFile(body)
call.enqueue(object : Callback<ResponseBody> {
override fun onFailure(call: retrofit2.Call<ResponseBody>, t: Throwable) {
Toast.makeText(this@RecitationActivity, "Upload Failed With The Throwable: $t", Toast.LENGTH_LONG).show()
}
override fun onResponse(
call: retrofit2.Call<ResponseBody>,
response: retrofit2.Response<ResponseBody>
) {
if (response.isSuccessful) {
// the upload was successful
Toast.makeText(this@RecitationActivity, "Upload Was A Successful", Toast.LENGTH_LONG).show()
} else {
// the upload was not successful
Toast.makeText(this@RecitationActivity, "Error uploading file", Toast.LENGTH_SHORT).show()
}
}
})
}
package com.example.qurantutor.network
import io.github.cdimascio.dotenv.dotenv
import okhttp3.Credentials
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
object RetrofitBuilderSmb {
private val service: Retrofit = Retrofit.Builder()
.baseUrl(BaseURL.smbbaseUrl)
.client(getSmbClient())
.addConverterFactory(GsonConverterFactory.create())
.build()
val smbService: SmbService = service.create(SmbService::class.java)
private fun getSmbClient(): OkHttpClient {
val dotenv = dotenv {
directory = "/assets"
filename = "env"
}
return OkHttpClient.Builder()
.authenticator { _, response ->
val username = "quranai"
val password = dotenv["passwd"]
response.request().newBuilder()
.header("Authorization", Credentials.basic(username, password))
.build()
}
.build()
}
}
package com.example.qurantutor.network
import okhttp3.MultipartBody
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part
interface SmbService {
@Multipart
@POST("/upload")
fun uploadFile(@Part file: MultipartBody.Part): Call<ResponseBody>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment