Solution 1 :
Try adding a HttpLoggingInterceptor to your OkHttpClient just to see what data is actually sent to the server:
val myClient = OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)
)
.build()
val api = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("yourURL")
.client(myClient)
.build()
.create(ApiClass::class.java)
You should then get an output like this and see what is actually sent to the server:
Connected to the target VM, address: '127.0.0.1:50563', transport: 'socket'
--> GET https://yourURL
--> END GET
<-- 200 OK https://yourURL (1129ms)
Server: nginx/1.16.1
Date: Sun, 07 Jun 2020 21:24:40 GMT
Content-Type: application/json
Content-Length: 197
Connection: keep-alive
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection
Problem :
I’m having some trouble with retrofit. I’m trying to send some data to my server, and the response is successful. However when I check if the data was sent, nothing was added. This works through postman though. Here is some code of my project:
Here is my model that it’s in my server.
Here is my endpoint:
interface Endpoint {
@FormUrlEncoded
@POST("api/ocorrenciasapi")
fun postOcorrencias(
@Field("Dispositivo") Dispositivo : String,
@Field("DataOcorrencia") DataOcorrencia : String,
@Field("Latitude") Latitude : String,
@Field("Longitude") Longitude : String,
@Field("Azimute") Azimute : String,
@Field("Fotografia") Fotografia : String,
@Field("NomeFotografia") NomeFotografia : String,
@Field("Estado") Estado : String
):Call<Ocorrencias>
}
Here is my RetrofitClient:
private const val BASE_URL = "https://adamastor.ipt.pt/appFogos/"
val instance: Endpoint by lazy{
val retrofit = Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
retrofit.create(Endpoint::class.java)
}
Here is my class Ocorrencias:
data class Ocorrencias
(
@SerializedName("Dispositivo")
var Dispositivo : String,
@SerializedName("DataOcorrencia")
var DataOcorrencia : String,
@SerializedName("Latitude")
var Latitude : String,
@SerializedName("Longitude")
var Longitude : String,
@SerializedName("Azimute")
var Azimute : String,
@SerializedName("Fotografia")
var Fotografia : String,
@SerializedName("nomeFotografia")
var nomeFotografia : String,
@SerializedName("Estado")
var Estado : String
)
Here is the post function that sends it to the server:
fun postData(){
val dispositivo : String = Settings.Secure.getString(
contentResolver,
Settings.Secure.ANDROID_ID
)
val fotografia : String = encoded
val nomefotografia : String = ""
val latitude : String = mLastLocation!!.latitude.toString()
val longitude : String =mLastLocation!!.longitude.toString()
val azimute : String = azimuth
btSubmeter.isEnabled=true
RetrofitClient.instance.postOcorrencias(
dispositivo,
"",
latitude,
longitude,
azimute,
"R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==",
nomefotografia,
"emAvaliacao"
).enqueue(object : Callback<Ocorrencias> {
override fun onFailure(call: Call<Ocorrencias>, t: Throwable) {
Toast.makeText(baseContext, "Ocorreu um erro", Toast.LENGTH_SHORT).show()
}
override fun onResponse(call: Call<Ocorrencias>, response: Response<Ocorrencias>) {
// Toast.makeText(applicationContext,"A ocorrência foi enviada", Toast.LENGTH_LONG).show()
success()
}
})
}
No errors occurred when I send the data to the server. I’m not sure why this is happening.
Comments
Comment posted by ZSergei
Maybe problem is in case sensitive naming? In request you use @Field(“NomeFotografia”) NomeFotografia : String. But in response @SerializedName(“nomeFotografia”).