改造@DELETE json

时间:2018-02-13 14:00:10

标签: android json retrofit retrofit2

所以我只有jsut开始使用Retrofit并试图了解它。 我创建了@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) {

        }
    });

然后我希望能够删除刚刚作为json发送的相同数据。我尝试使用:

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

有了这个,我只是将api.postTest更改为api.deleteTest并崩溃了应用程序?我不能使用ID,因为每个ID都使用UUID。服务器将保存多个数据。

感谢任何帮助。

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)

1 个答案:

答案 0 :(得分:0)

试试这个:

@HTTP(method = "DELETE", path = "/TEST", hasBody = true)
Call<ResponseBody> deleteTest(@Body RequestBody requestBody);