gpt4 book ai didi

How to upload files to bunnyCDN storage zone using retrofit kotlin Android?(如何使用改装的Kotlin Android将文件上传到bunnyCDN存储区?)

转载 作者:bug小助手 更新时间:2023-10-28 21:26:16 26 4
gpt4 key购买 nike



I want to upload a video to BunnyCDN using Retrofit in Android Kotlin, but getting crashes and error is not specified properly in Android Studio. Where I have made the mistake

我想上传一个视频到BunnyCDN使用改装在Android Kotlin,但得到崩溃和错误没有正确指定在Android Studio。我在哪里犯了错


Here is the documentation

这是文档


[https://docs.bunny.net/reference/put_-storagezonename-path-filename][1]

Here is my all code.

这是我的所有代码。


In Main Activity

在主要活动中


@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding
private val REQUEST_CODE: Int = 0
private lateinit var uploadViewModel : VideoUploadViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

uploadViewModel = ViewModelProvider(this).get(VideoUploadViewModel::class.java)

val contact = registerForActivityResult(ActivityResultContracts.GetContent()){
binding.videoUri.text = it.toString()
uploadVideoToBunnyCDN(it!!)
}

binding.selectVideoBtn.setOnClickListener{
contact.launch("video/*")
}

uploadViewModel.videoUploadLiveData.observe(this) {
Log.i("message", it.message.toString())
}

}

private fun uploadVideoToBunnyCDN(it: Uri) {
val file = File(it.path) // Get the file from the Uri
val mediaType = "application/octet-stream".toMediaTypeOrNull()
val requestBody = RequestBody.create(mediaType, file)
Log.i("VIDEO_DATA_", requestBody.contentType().toString())
uploadViewModel.uploadVideo("assam-storage-zone","myFolder","videNew", requestBody)
}

}

ViewModel Class

ViewModel类


@HiltViewModel
class VideoUploadViewModel @Inject constructor(private val repository: Repository) : ViewModel(){

val videoUploadLiveData get() = repository.statusLiveData

fun uploadVideo(storageZoneName: String,
folderName: String,
fileName: String,
file: RequestBody) {

viewModelScope.launch {
repository.uploadVideo(storageZoneName, folderName, fileName, file)
}
}

}

Repository class

存储库类


class Repository @Inject constructor(private val uploadApi: UploadApi)  {

private val _statusLiveData = MutableLiveData<NetworkResult<Pair<Boolean, String>>>()
val statusLiveData get() = _statusLiveData

suspend fun uploadVideo(storageZoneName: String,
folderName: String,
fileName: String,
file: RequestBody
) {
_statusLiveData.postValue(NetworkResult.Loading())
val response = uploadApi.uploadVideo(storageZoneName, folderName, fileName, file)
handleResponse(response, "Video Uploaded")
}

private fun handleResponse(response: Response<VideoUploadResponse>, message: String) {
if (response.isSuccessful && response.body() != null) {
_statusLiveData.postValue(NetworkResult.Success(Pair(true, message)))
} else {
_statusLiveData.postValue(NetworkResult.Success(Pair(false, "Something went wrong")))
}
}

}

Interface

接口


interface UploadApi {

@Multipart
@PUT("/{storageZoneName}/{path}/{fileName}")
fun uploadVideo(
@Path("storageZoneName") storageZoneName: String,
@Path("path") folderName: String,
@Path("fileName") fileName: String,
@Body videoData: RequestBody
)
: Response<VideoUploadResponse>

}

AuthInterceptor class

AuthInterceptor类


class AuthInterceptor @Inject constructor(): Interceptor {

/*This will observe the request and add modification before sending the request*/
override fun intercept(chain: Interceptor.Chain): Response {

/*This will give us a new request object that we will be modified*/
val request = chain.request().newBuilder()

request.addHeader("AccessKey", "my access key")
request.addHeader("content-type", "application/octet-stream")

//return the modified request
return chain.proceed(request.build())
}
}

NetworkModule class

NetworkModule类


@InstallIn(SingletonComponent::class)
@Module
class NetworkModule {

/*This function will be used by hilt to create Retrofit object. Called by hilt*/
@Singleton
@Provides
fun providesRetrofitBuilder(): Retrofit.Builder{
return Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("https://storage.bunnycdn.com/")

}

@Singleton
@Provides
fun providesOkhttpClient(authInterceptor: AuthInterceptor) : OkHttpClient {
return OkHttpClient.Builder().addInterceptor(authInterceptor).build()
}


@Singleton
@Provides
fun providesNoteAPI(retrofitBuilder: Retrofit.Builder, okHttpClient: OkHttpClient) : UploadApi{
return retrofitBuilder
.client(okHttpClient)
.build()
.create(UploadApi::class.java)
}

}

VideoUploadResponse class

VideoUploadResponse类


data class VideoUploadResponse(
val message: String,
val HttpCode: Boolean
)

NetworkResult class

NetworkResult类


 sealed class NetworkResult<T>(val data: T?= null, val message: String?= null){
class Success<T>(data: T): NetworkResult<T>(data)
class Error<T>(message: String?, data: T?= null): NetworkResult<T>(data, message)
class Loading<T> : NetworkResult<T>()
}

更多回答
优秀答案推荐
更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com