Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android studio – I’m having some trouble with retrofit and sending some data to the server

Posted on November 11, 2022 By

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.
enter image description here

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.

READ  [FIXED] android - Populating a RecycleView with images from Storage References with Glide
Powered by Inline Related Posts

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”).

Android Tags:android-studio, kotlin, server

Post navigation

Previous Post: [FIXED] java – How to handle large amount of animated vector drawables?
Next Post: [FIXED] c# – Call a self hosted web api from an Android app through a DDNS

Related Posts

[FIXED] android – how to send text to server and get array with volley and json Android
[FIXED] android – Could not find a generator for route RouteSettings(“/First”, null) in the _WidgetsAppState Android
[FIXED] java – View Binding causes “The specified child already has a parent” Android
[FIXED] android – push notification not received to devices when app is killed Android
[FIXED] android – Why doesn’t the “@string/” attribute not have a + sign? Android
[FIXED] android – Is it possible to get an on dynamic feature module size before starting to download it? Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme