I have a problem with “No Retrofit annotation found. (parameter #2)”, when calling the POST method in android api service. At the beginning, maybe I will present what I want to get and then the code.
I wanted to use the POST method to upload the object to the server from kotlin but when I call the function, I get the problem that I mentioned earlier. What can I do to fix this?
Some code
Repo:
suspend fun addMonument(monuments: Monuments){
withContext(Dispatchers.IO) {
try {
CityVisitorApi.RETROFIT_SERVICE.addMonument(monuments)
} catch (e: Exception) {
Log.e("network", e.message)
}
}
}
Service:
private const val BASE_URL = "http://192.168.0.43:8080/api/v1/"
interface CityVisitorApiService {
@Headers("Content-Type: application/json")
@POST("monuments/add")
suspend fun addMonument(@Body monuments: Monuments)
}
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.baseUrl(BASE_URL)
.build()
object CityVisitorApi {
val RETROFIT_SERVICE : CityVisitorApiService by lazy { retrofit.create(CityVisitorApiService::class.java) }
}
creator:
class MonumentCreator {
var name = ""
var description = ""
var category = ""
var url = ""
var rate = 0.0f
var lat = 0.0
var lng = 0.0
fun createMonument() : Monuments{
return Monuments(
name = name,
info = description,
category = category,
website = url,
rate = rate,
geo = LatLong(lat,lng)
)
}
}
viewModel:
private val repository = MonumentsRepository(CityVisitorApi.RETROFIT_SERVICE)
var monumentCreator = MonumentCreator()
fun addMonument() {
viewModelScope.launch {
name.value?.let { monumentCreator.name = it }
description.value?.let { monumentCreator.description = it }
category.value?.let { monumentCreator.category = it }
url.value?.let { monumentCreator.url = it }
rate.value?.let { monumentCreator.rate = it.toFloat() }
lat.value?.let { monumentCreator.lat = it.toDouble() }
lng.value?.let { monumentCreator.lng = it.toDouble() }
val monuments = monumentCreator.createMonument()
repository.addMonument(monuments)
}
}
And at the back-end server POST method that I want to call from kotlin:
@PostMapping(path = "/add")
public void addNewMonument(@RequestBody MonumentDTO monument){
monumentService.addNewMonument(converter.dtoToEntity(monument));
}
I don’t know if it has anything to do with it, but the same error shows up even when the server is not running.