代码之家  ›  专栏  ›  技术社区  ›  Kamal Kakkar

如何使用改装发布(x-www-form-urlencoded)Json数据?[副本]

  •  4
  • Kamal Kakkar  · 技术社区  · 6 年前

    当我在body(x-www-form-urlencoded)中发布数据时,它在Postman中运行良好。但使用改型的2.0安卓系统则无法实现。

    @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);
    

    此代码无效。我也尝试使用@FormUrlEncoded。

    1 回复  |  直到 6 年前
        1
  •  11
  •   Navneet Krishna    6 年前

    尝试使用 @FormUrlEncoded 和使用 @Field 而不是 @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");