i was able to make Edit and Delete work in a test project, using Retrofit2, but now i needed to used it in my real project, and despite the code and webservice used (created using Slim 4 and Notorm) are the same, both Edit and Delete started not working for some reason (get and Post both work correctly)
For both Edit i am using a page that get all the date from the DB and put it in some views, and then i click in a button that have the Edit Function . I tried debugging and everything is fine, until it tries to enter the call.enqueue method, and it fails, without giving any error on my logcat. I have a toast on my onResponse and another toast on my OnFailure and neither shows on my screen.
With the Delete the situation is similar i have a page, with a call.enqueue (on the on.create) that gets all the data that appears in the views, and then a button that trigger the delete method, and here also the call.enqueue doens't work. I tested this, without having the call.enqueue on the oncreate, but it happens the same thing.
Here is my Edit button code
fun editar(view: View) {
val request = ServiceBuilder.buildService(EndPoints::class.java)
val latitude = latitude.text.toString().toDouble()
val longitude = longitude.text.toString().toDouble()
val morada = editMoradaView.text.toString()
val n_quartos = editNQuartosView.text.toString().toInt()
val casaBanho = casaBanho.text.toString().toInt()
val contacto = contacto.text.toString()
val mobilada = mobilada.text.toString()
val preco = preco.text.toString().toDouble()
val observacao = observacao.text.toString()
val utilizador_id = shared_preferences.getInt("id", 0)
if (isBitMap) {
val base = getBase64String(decodedByte)
fotografia = base
} else {
fotografia = base64
}
val call = request.editar(
id = ID,
users_id = utilizador_id,
morada = morada,
n_quartos = n_quartos,
latitude = latitude.toString().toDouble(),
longitude = longitude.toString().toDouble(),
fotografia = fotografia,
preco = preco,
ncasas_banho = casaBanho,
telemovel = contacto,
mobilado = mobilada,
outros_atributos = observacao
)
call.enqueue(object : Callback<OutputEditar> {
override fun onResponse(call: Call<OutputEditar>, response: Response<OutputEditar>) {
if (response.isSuccessful) {
val c: OutputEditar = response.body()!!
Toast.makeText(this@EditarAnuncios, c.MSG, Toast.LENGTH_LONG).show()
val intent = Intent(this@EditarAnuncios, MapsActivity::class.java)
startActivity(intent)
finish()
}
}
override fun onFailure(call: Call<OutputEditar>, t: Throwable) {
Toast.makeText(this@EditarAnuncios, "${t.message}", Toast.LENGTH_SHORT).show()
}
})
}
Here is my Delete Button Code
fun delete(view: View) {
var id = intent.getStringExtra("PARAM_ID")
var idString = id.toString()
val request = ServiceBuilder.buildService(EndPoints::class.java)
val call = request.apagarAnuncio(id = idString.toInt())
call.enqueue(object : Callback<OutputApagar> {
override fun onResponse(call: Call<OutputApagar>, response: Response<OutputApagar>) {
if (response.isSuccessful) {
val c: OutputApagar = response.body()!!
Toast.makeText(this@DetalhesAnuncioLogado, c.MSG, Toast.LENGTH_LONG).show()
val intent = Intent(this@DetalhesAnuncioLogado, MapsActivity::class.java)
setResult(Activity.RESULT_OK, intent)
finish()
}
}
override fun onFailure(call: Call<OutputApagar>, t: Throwable) {
Toast.makeText(this@DetalhesAnuncioLogado, "${t.message}", Toast.LENGTH_SHORT)
.show()
}
})
}
Here are my 2 data classes (in separated files)
data class OutputEditar(
val users_id: Int,
val morada: String,
val n_quartos: Int,
val latitude: Double,
val longitude: Double,
val fotografia: String,
val preco: Double,
val ncasas_banho: Int,
val telemovel: String,
val mobilado: String,
val outros_atributos: String,
val status: String,
val MSG: String
)
data class OutputApagar(
val status: String,
val MSG: String
)
Here are my endpoints
@FormUrlEncoded
@POST("/editar_anuncios/{id}")
fun editar(@Path("id") id: Int?,
@Field("users_id") users_id: Int?,
@Field("morada") morada: String?,
@Field("n_quartos") n_quartos: Int?,
@Field("latitude") latitude: Double?,
@Field("longitude") longitude: Double?,
@Field("fotografia") fotografia: String?,
@Field("preco") preco: Double?,
@Field("ncasas_banho") ncasas_banho: Int?,
@Field("telemovel") telemovel: String?,
@Field("mobilado") mobilado: String?,
@Field("outros_atributos") outros_atributos: String?): Call<OutputEditar>
@POST("/apagarAnuncios/{id}")
fun apagarAnuncio(@Path("id") id: Int?): Call<OutputApagar>
Both Endpoints work well on my Test Project and on Postman
My Service Builder
object ServiceBuilder {
private val client = OkHttpClient.Builder().build()
private val retrofit = Retrofit.Builder()
.baseUrl("https://tneveda.000webhostapp.com/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
fun<T> buildService(service: Class<T>): T {
return retrofit.create(service)
}
}
Thank You in advance
That moment, when 10 minutes i made this Question, i found out my problem. Forgot to update my Endpoints on my Real Project, and that is why it was not working