Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post (x-www-form-urlencoded) Json Data using Retrofit? [duplicate]

When I post data in body (x-www-form-urlencoded) it works fine in Postman. But using Retrofit 2.0 Android it does not work.

@Headers("Content-Type: application/x-www-form-urlencoded")
@POST("/api/jsonws/pushUserData")
Call<ResponseBody>  pushData(@Body JSONObject jsonObj);

JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("role","owner");
            jsonObject.put("id","27001");

        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiInterface apiInterface1= ApiClient.getClientAuthentication().create(ApiInterface.class);
        Call<ResponseBody> responseBodyCall = apiInterface1.pushData(jsonObject);

This code not work.I also try @FormUrlEncoded.

like image 939
Kamal Kakkar Avatar asked Dec 24 '22 08:12

Kamal Kakkar


1 Answers

Try using @FormUrlEncoded and use @Field instead of @Body

@FormUrlEncoded
@POST("/api/jsonws/pushUserData")
Call<ResponseBody>  pushData(@Field("role") String role, @Field("id") String id);

ApiInterface apiInterface1= ApiClient.getClientAuthentication().create(ApiInterface.class);
Call<ResponseBody> responseBodyCall = apiInterface1.pushData("owner","27001");
like image 155
Navneet Krishna Avatar answered Dec 25 '22 21:12

Navneet Krishna