-1

So I have only jsut started using Retrofit and trying to get my head around it. I have created a @POST:

String suuid = UUID.randomUUID().toString();

    String json = "{\"id\" : \""+suuid+ "\", \"sensorType\" : \"sound\", \"threshold\" : \"50\", \"operator\" : \">\", \"actuator\" : \"1\", \"actuatorAction\" : \"4\"}";
    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),json);
    api.postTest(requestBody).enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {

        }
    });

I then want to be able to delete the same data that was just sent as json. I tried using:

@DELETE("/TEST")
Call<ResponseBody> deleteTest(@Body RequestBody requestBody);

With this I simply changed the api.postTest to api.deleteTest and crashed the app? I cant use ID as each ID is using UUID. And server would hold multiple data entrys.

Any help is appreciated.

ERROR:
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: rsstudio77.omnisciencehome, PID: 15088
              java.lang.RuntimeException: Unable to start activity ComponentInfo{rsstudio77.omnisciencehome/rsstudio77.omnisciencehome.security.securityPreConfig}: java.lang.IllegalArgumentException: Non-body HTTP method cannot contain @Body.
                  for method Api.deleteTest
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                  at android.os.Handler.dispatchMessage(Handler.java:106)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6494)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
               Caused by: java.lang.IllegalArgumentException: Non-body HTTP method cannot contain @Body.
                  for method Api.deleteTest
                  at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:752)
                  at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:743)
                  at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:211)
                  at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
                  at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
                  at java.lang.reflect.Proxy.invoke(Proxy.java:913)
                  at $Proxy0.deleteTest(Unknown Source)
                  at rsstudio77.omnisciencehome.security.securityPreConfig.onCreate(securityPreConfig.java:58)
                  at android.app.Activity.performCreate(Activity.java:6999)
                  at android.app.Activity.performCreate(Activity.java:6990)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
Rikhil Shah
  • 58
  • 5
  • 10

1 Answers1

2

Try this:

@HTTP(method = "DELETE", path = "/TEST", hasBody = true)
Call<ResponseBody> deleteTest(@Body RequestBody requestBody);
Joe Rakhimov
  • 4,713
  • 9
  • 51
  • 109
  • `path` is not mandatory if your first argument to interface method is a url annotated with @Url . Originally answered here : https://stackoverflow.com/a/62920127/8956093 – Ramakrishna Joshi Jul 15 '20 at 19:24