0

I'm developing an Android app using Retrofit to connect to a Spring Boot server. When I update data, there are extra slashes and double quotes on the server.
This is the output of POST method. "open"
This is the output of PUT method. "\"open\""
I read a similar article and I'm guessing I encode twice, but I don't know where I'm doing it. Please help me.
This is the service class of Android.

    @PUT("/posts/close/update/{id}")
    fun updateClose(@Path("id") id: Long, @Body close: String): Call<ResponseBody>

This is the view.

onClick = {
  val moshi = Moshi.Builder()
          .add(KotlinJsonAdapterFactory())
          .build()

  val retrofit = Retrofit.Builder()
          .baseUrl("https://*****.com")
          .addConverterFactory(MoshiConverterFactory.create(moshi))
          .build()

    thread {
        try {
            val service: PostService =
                retrofit.create(PostService::class.java)

            service.updateClose(6, "open")
                .enqueue(object : Callback<ResponseBody> {
                    override fun onResponse(
                        call: Call<ResponseBody>,
                        response: Response<ResponseBody>
                    ) {
                        Log.d("Response is", "${response.body()}")
                    }

                    override fun onFailure(
                        call: Call<ResponseBody>,
                        t: Throwable
                    ) {
                        Log.d("Hi", "error")
                    }
                })
        } catch (e: Exception) {
            Log.d("response-weather", "debug $e")
        }
    }

This is the repository of Spring Boot.

@Modifying
@Transactional
@Query("UPDATE posts SET close = :close where post_id = :id", nativeQuery = true)
fun updateClose(@Param("id") id: Long, @Param("close") close: String)

Thank you very much.

Yuki
  • 225
  • 2
  • 7
  • 1
    This isn't just "extra slashes" as your title claims. You are seeing a string which contains double quotes. Beacause `"` is used to surround the string, we must use `\"` to represent a quote inside the string. You need to determine why these quotes are part of the string. Where are you getting this output from? That's the place to start, then work backwards from there. – Code-Apprentice Jan 03 '23 at 04:26
  • I see. But when I post data, there are not \"...\" on the server. So, there is a difference in format when I post data and update data. This causes a problem when I get data from the server using queries. – Yuki Jan 03 '23 at 04:42
  • How are you viewing this data? – Code-Apprentice Jan 03 '23 at 04:54
  • On Web API which I'm creating. I tried to use curl to update data and it works fine(The result is without extra \"). So I assume there is something wrong with Android side. – Yuki Jan 03 '23 at 05:18
  • 1
    So you see the extra quotes in the data that your server recieves? Then the next step is to debug your Android code to see where it adds these quotes to the request body. What code builds the request? – Code-Apprentice Jan 03 '23 at 07:12
  • I think the `updateClose()` method in your Retrofit interface does the PUT request, right? You call it with `service.updateClose(6, "open")` which looks correct. Is there anywhere else in your code that you call `updateClose()`. – Code-Apprentice Jan 03 '23 at 07:15
  • That's right. updateClose() does the PUT request. On the same view, I use this method one more time, but the only difference is the second parameter. I use "close" here. The result is the same, with the extra quotes. I've just posted the curl methods I used to put and post. I noticed the difference between them so I hope this is a hint to solve this problem. – Yuki Jan 03 '23 at 09:30
  • Just in case, for POST, I'm using save method in JPA repository. On the other hand, I'm using native query for PUT. – Yuki Jan 03 '23 at 09:37
  • What do you do that causes the extra quotes to appear? – Code-Apprentice Jan 04 '23 at 16:27
  • @Code-Apprentice Sorry for the late reply. I was struggling with this issue. I've just posted where I corrected to make it work fine. Thanks! – Yuki Jan 08 '23 at 02:42
  • You should post the solution below in the answer section instead of editing your question. – Code-Apprentice Jan 08 '23 at 17:00

2 Answers2

2

There is nothing wrong with the data or the android side.

Strings in JSON must be written in double quotes. For more info refer this page.

Your JSON data is {"name": "Ken", "uid": "12345"}

In order to use double quotes inside a string you have to escape it via a backslash. For more info refer this question.

That's the reason for the extra backslashes.

I tried to load the json string via python and it worked like a charm. Attaching screenshot for reference. So any backend you would be using will be able to parse the JSON String.

screenshot

SURYA S R
  • 320
  • 8
0

Finally, I got the codes which work fine.
Service Class of Android.

@PUT("/posts/close/update/{id}")
    fun updateClose(@Path("id") id: Long, @Query("close") close: String): Call<ResponseBody>

Controller class of Spring Boot. Before, I used @RequestBody instead of @RequestParam.

@PutMapping("/posts/close/update/{id}")
    fun updateClose(@PathVariable id: Long, @RequestParam close: String) = postService.updateClose(id, close)
Yuki
  • 225
  • 2
  • 7