How to POST raw whole JSON in the body of a Retrofit request?

前端 未结 23 2712
面向向阳花
面向向阳花 2020-11-22 00:57

This question may have been asked before but no it was not definitively answered. How exactly does one post raw whole JSON inside the body of a Retrofit request?

See

23条回答
  •  野的像风
    2020-11-22 01:58

    Using JsonObject is the way it is:

    1. Create your interface like this:

      public interface laInterfaz{ 
          @POST("/bleh/blah/org")
          void registerPayer(@Body JsonObject bean, Callback callback);
      }
      
    2. Make the JsonObject acording to the jsons structure.

      JsonObject obj = new JsonObject();
      JsonObject payerReg = new JsonObject();
      payerReg.addProperty("crc","aas22");
      payerReg.addProperty("payerDevManufacturer","Samsung");
      obj.add("payerReg",payerReg);
      /*json/*
          {"payerReg":{"crc":"aas22","payerDevManufacturer":"Samsung"}}
      /*json*/
      
    3. Call the service:

      service.registerPayer(obj, callBackRegistraPagador);
      
      Callback callBackRegistraPagador = new Callback(){
          public void success(JsonObject object, Response response){
              System.out.println(object.toString());
          }
      
          public void failure(RetrofitError retrofitError){
              System.out.println(retrofitError.toString());
          }
      };
      

    And that its! In my personal opinion, its a lot better than making pojos and working with the class mess. This is a lot more cleaner.

提交回复
热议问题