【发布时间】:2020-06-08 05:27:25
【问题描述】:
我在改造时遇到了一些麻烦。我正在尝试向我的服务器发送一些数据,并且响应成功。但是,当我检查数据是否已发送时,没有添加任何内容。不过,这可以通过邮递员工作。这是我项目的一些代码:
这是我的终点:
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>
}
这是我的 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)
}
这是我的 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
)
这里是发送到服务器的 post 函数:
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()
}
})
}
当我将数据发送到服务器时没有发生错误。我不确定为什么会这样。
【问题讨论】:
-
也许问题出在区分大小写的命名上?在请求您使用 @Field("NomeFotografia") NomeFotografia :字符串。但作为回应@SerializedName("nomeFotografia")。
标签: android-studio kotlin server