【问题标题】:I'm having some trouble with retrofit and sending some data to the server我在改造和向服务器发送一些数据时遇到了一些问题
【发布时间】: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


【解决方案1】:

尝试将 HttpLoggingInterceptor 添加到 OkHttpClient 以查看实际发送到服务器的数据:

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)

然后您应该得到这样的输出,并查看实际发送到服务器的内容:

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

【讨论】:

    猜你喜欢
    • 2013-10-08
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 2022-11-23
    相关资源
    最近更新 更多