About 424 error while post json using retrofit in android studio to azure API

50 views Asked by At

I got some problems when I wanted to use POST API Method to Azure API, I thought I'd write code correctly but that was still wrong, I've tested the POST API using Postman, and that was successful, but when I POST using Retrofit i got 424 Error: Failed Dependencies There is JSON structure in Azure API

{
  "input_data": {
    "columns": ["Humidity", "Temperature", "Step count"],
    "index": [1],
    "data": [[25.0, 30.5, 500.0]]
  }
}

i just want to post "data" and there is my data Request

data class RequestData(
    val input_data: InputData
)


data class InputData(
    val columns: List<String>,
    val index: List<Int>,
    val data: ArrayList<ArrayList<Float>>
)

There is my API Service Interface

interface APIService {
    @Headers("Content-Type: application/json", "Authorization: Bearer my-token")
    @POST("/score")
    fun postData(
        @Body requestData: String
    ): Call<PostResponse>
}

for addition, this is my ObjectClient code

object ObjectClient {
    val client : APIService by lazy {
        val retrofit = Retrofit.Builder()
            .baseUrl("https://my-api-base-URL")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        retrofit.create(APIService::class.java)
    }
}

and for my main activity, i'm using button as listener to send post request, i code like this, i use editText View to input data value

        sendDataButton.setOnClickListener {
            val humidity = humidityTextView.text.toString()
            val temperature = temperatureTextView.text.toString()
            val stepCount = stepCountTextView.text.toString()

            val inputData = InputData(
                columns = listOf("Humidity", "Temperature", "Step count"), // Use uppercase column names
                index = listOf(0),
                data = arrayListOf(arrayListOf(humidity.toFloat(), temperature.toFloat(), stepCount.toFloat()))
            )

            val requestData = RequestData(input_data = inputData)

            val requestDataJson = gson.toJson(requestData)

            sendRequestToAPI(requestDataJson)

        }

and my send requestToAPI function i code like this

   private fun sendRequestToAPI(requestData: String) {
        apiService.postData(requestData).enqueue(object : Callback<PostResponse> {
            override fun onResponse(call: Call<PostResponse>, response: Response<PostResponse>) {
                val statusCode = response.code() // Get the HTTP status code

                if (response.isSuccessful) {
                    val message = response.body()?.message ?: "OK"
                    val jsonResponse = gson.toJson(requestData) // Assuming you have a Gson instance named 'gson'
                    Log.d("API Response", jsonResponse)
                    responseList.add("Status Code: $statusCode\n$message\n" +
                            jsonResponse)

                    // Log or print the JSON response here

                } else {
                    val jsonResponse = gson.toJson(requestData) // Assuming you have a Gson instance named 'gson'
                    Log.d("API Response", jsonResponse)
                    responseList.add("Status Code: $statusCode\nFailed to send data\n" +
                            jsonResponse)
                }
                adapter.notifyDataSetChanged()
            }

for my result in android studio (Failed try) is like this, i've search some solution for this like, correct json structure, add internet permission in manifest, correct request data type in data class , create json data using gson similar to json structure in azure api before but it still 424 error

enter image description here

and there is the result using postman (Success try)

enter image description here

I,m sorry for the long question, cause i'm in learning using android studio, and i'm still not good in android studio yet, I'm so thanksfull if you help my problem, cause this is my project for my college. for additional information, i this is combination code resource from youtube, github and some AI chat like ChatGPT

Thank you

I've search for some solution for this like, correct JSON structure, adding internet permission in the manifest, correcting request data type in the data class , and creating JSON data using GSON similar to JSON structure in Azure API before but it still has 424 error

what I expecting that code can running and can send data to Azure API without any bug and error

0

There are 0 answers