1. 發(fā)送表單數(shù)據(jù)(Form-Encoded)
當需要向服務器提交表單數(shù)據(jù)時,可以使用 @FormUrlEncoded 和 @Field 或 @FieldMap 注解。
@FormUrlEncoded 注解用于標識請求體為表單編碼格式。
@Field 注解用于指定表單字段的名稱和值。
// 定義 API 接口
interface ApiService {
@FormUrlEncoded
@POST("login")
fun login(@Field("username") username: String,@Field("password") password: String
): Call<LoginResponse>
}
// 響應數(shù)據(jù)類
data class LoginResponse(val message: String)
// 使用示例
fun main() {
val retrofit = Retrofit.Builder()
.baseUrl("https://example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
val apiService = retrofit.create(ApiService::class.java)
val call = apiService.login("testuser", "testpassword")
call.enqueue(object : retrofit2.Callback<LoginResponse> {
override fun onResponse(
call: Call<LoginResponse>,
response: retrofit2.Response<LoginResponse>
) {
if (response.isSuccessful) {
val loginResponse = response.body()
println("登錄成功: ${loginResponse?.message}")
} else {
println("登錄失敗: ${response.code()}")
}
}
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
println("請求出錯: ${t.message}")
}
})
}
- 發(fā)送 JSON 數(shù)據(jù)
如果需要發(fā)送 JSON 格式的數(shù)據(jù),可以使用 @Body 注解將一個對象作為請求體發(fā)送。
@Body 注解用于將 LoginRequest 對象作為請求體發(fā)送,Retrofit 會自動將對象轉換為 JSON 格式。
// 請求數(shù)據(jù)類
data class LoginRequest(val username: String, val password: String)
// 響應數(shù)據(jù)類
data class LoginResponse(val message: String)
// 定義 API 接口
interface ApiService {
@POST("login")
fun login(@Body loginRequest: LoginRequest): Call<LoginResponse>
}
// 使用示例
fun main() {
val retrofit = Retrofit.Builder()
.baseUrl("https://example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
val apiService = retrofit.create(ApiService::class.java)
val loginRequest = LoginRequest("testuser", "testpassword")
val call = apiService.login(loginRequest)
call.enqueue(object : retrofit2.Callback<LoginResponse> {
override fun onResponse(
call: Call<LoginResponse>,
response: retrofit2.Response<LoginResponse>
) {
if (response.isSuccessful) {
val loginResponse = response.body()
println("登錄成功: ${loginResponse?.message}")
} else {
println("登錄失敗: ${response.code()}")
}
}
override fun onFailure(call: Call<LoginResponse>, t: Throwable) {
println("請求出錯: ${t.message}")
}
})
}
- 發(fā)送多部分數(shù)據(jù)(Multipart)
當需要上傳文件或同時上傳文件和其他表單數(shù)據(jù)時,可以使用 @Multipart、@Part 或 @PartMap 注解。
@Multipart 注解用于標識請求體為多部分表單數(shù)據(jù)。
@Part 注解用于指定每個部分的數(shù)據(jù),如文件或表單字段。
// 響應數(shù)據(jù)類
data class UploadResponse(val message: String)
// 定義 API 接口
interface ApiService {
@Multipart
@POST("upload")
fun uploadFile(
@Part file: MultipartBody.Part,
@Part("description") description: RequestBody
): Call<UploadResponse>
}
// 使用示例
fun main() {
val retrofit = Retrofit.Builder()
.baseUrl("https://example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()
val apiService = retrofit.create(ApiService::class.java)
// 創(chuàng)建文件請求體
val file = java.io.File("path/to/your/file.jpg")
val requestFile = RequestBody.create(okhttp3.MediaType.parse("image/jpeg"), file)
val filePart = MultipartBody.Part.createFormData("file", file.name, requestFile)
// 創(chuàng)建描述請求體
val description = RequestBody.create(okhttp3.MediaType.parse("text/plain"), "This is a test file")
val call = apiService.uploadFile(filePart, description)
call.enqueue(object : retrofit2.Callback<UploadResponse> {
override fun onResponse(
call: Call<UploadResponse>,
response: retrofit2.Response<UploadResponse>
) {
if (response.isSuccessful) {
val uploadResponse = response.body()
println("上傳成功: ${uploadResponse?.message}")
} else {
println("上傳失敗: ${response.code()}")
}
}
override fun onFailure(call: Call<UploadResponse>, t: Throwable) {
println("請求出錯: ${t.message}")
}
})
}