3

I am trying to make the Api call using Retrofit 2 POST method with @Field and @FormurlEncoded in Android but it just doesn't work in Android PIE.

I have integrated Apis using retrofit thousands of time before. But this time I have targeted sdk Android Pie with retrofit version 2.6.1.

The strange thing is the same thing is working perfectly fine with @Body.

So I have gone through following things to check it out but no luck. I also thought that Android 9.0 might causing the security issue but then @Body part should also not be able to run successfully.

https://square.github.io/retrofit/

Android 8: Cleartext HTTP traffic not permitted

How to make https request with ssl certificate in Retrofit

https://developer.android.com/training/articles/security-config.html

My configurations

targetSdkVersion 29

retrofit2Version = '2.6.1'

Url - https://jsonplaceholder.typicode.com/posts

Request (Which is not working - WHY ?)

@Headers({"Content-Type: application/json",
            "Cache-Control: no-cache"})
    @FormUrlEncoded
    @POST("/posts")
    Call<BinRespTempPost> post(@Field("title") String title,
                               @Field("body") String body,
                               @Field("userId") int userId);

Request which is working

@Headers({"Content-Type: application/json",
            "Cache-Control: no-cache"})
    @POST("/posts")
    Call<BinRespTempPost> post(@Body BinReqTempPost binReqTempPost);

Response

{
  id: 101,
  title: 'foo',
  body: 'bar',
  userId: 1
}

The question is why Api call is successful with @Body and Failure with @Field / @FormUrlEncoded ?

--------- UPDATE ----------

URL type - POST

Response on failure - Html response (500 error)

Sagar Shah
  • 4,272
  • 2
  • 25
  • 36
  • May I know what is your expected response looks? I guess the URL is GET type so you won't need to add request parameters. – DHAVAL A. Aug 27 '19 at 06:21
  • Hi @DHAVALASODARIYA URL is https POST type, And response is in html format when gets failure. – Sagar Shah Aug 27 '19 at 06:31
  • Please check these images. [Get](https://drive.google.com/open?id=1MHTeNWH6_Ug12iDzKZzaJq2Ale0JWAHF) request and [Post](https://drive.google.com/open?id=1NsyF8VcTbGoecPi1lu4BI9pTI1ucnKA6) request. It shows that your URL is works perfectly fine with GET. – DHAVAL A. Aug 27 '19 at 06:48
  • Check the source of this APi here - https://jsonplaceholder.typicode.com/guide.html I want this to work with Post method as it is originally POST method. Otherwise you can check any Post method with https url with my posted configuration. – Sagar Shah Aug 27 '19 at 06:56
  • Have you tried with removing header? – DHAVAL A. Aug 27 '19 at 07:17
  • Yes it doesn't matter. If you really want to try then you can use any of your own https post url. And you will be able to reproduce it this scenario. – Sagar Shah Aug 27 '19 at 07:25
  • It´s your webservice accepting x-www-form-urlencoded requests? – reixa Aug 27 '19 at 08:34
  • @axierjhtjz yes – Sagar Shah Aug 27 '19 at 09:00
  • Did you find any solution yet? – Mohammad May 01 '20 at 09:13
  • Not deep dived in to it yet. May be they have sorted it in latest Retrofit version. – Sagar Shah May 04 '20 at 09:14

0 Answers0