1

I have tried everything I can but it still doesn't work. I have already use okhttp 2.0 and okhttp-urlconnection 2.0 combined with retrofit 1.9. The Delete Body still doesn't work and I've already check all the issue posted before for this problem but I still can't find the answer. Is there something I am doing wrong?

This is what my interface looks like :

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@RestMethod(value = "DELETE", hasBody = true)
@interface MY_DELETE {
    String value();
}

public interface WebServiceMethods {

@FormUrlEncoded
@Headers("Content-Type: application/json")
@MY_DELETE("/DeleteLoggedHours")
void deleteLoggedHour(@Body ProjectTask projectTask, Callback<SuccessSubmissionChecker> successSubmissionCheckerCallback);}

as for my WebSevice, it looks like this :

public class WebService {

final private static String BASE_URL = "http://172.17.24.26:758/Service1.svc";

public static WebServiceMethods webServiceMethods;

private OkHttpClient getClient() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(5, TimeUnit.MINUTES);
    client.setReadTimeout(5, TimeUnit.MINUTES);
    return client;
}

public static WebServiceMethods getWebServiceMethods() {

    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setReadTimeout(2, TimeUnit.MINUTES);
    okHttpClient.setConnectTimeout(2, TimeUnit.MINUTES);

    if (webServiceMethods == null){
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(BASE_URL)
                .setClient(new OkClient(okHttpClient))
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setLog(new RestAdapter.Log() {
                    @Override
                    public void log(String msg) {
                      Log.e("RestAdapter", msg);
                    }
                })
                .build();

        webServiceMethods = restAdapter.create(TimeTrackerWebServiceMethods.class);
    }

    return webServiceMethods;
}

This is where I call the method :

public void deleteLoggedHour(ProjectTask projectTask, final MainFragment.DeleteCheckerCallback deleteCallback) {

     WebServiceMethods.deleteLoggedHour(projectTask, new Callback<SuccessSubmissionChecker>() {
         @Override
         public void success(SuccessSubmissionChecker submissionChecker, Response response) {
             Log.e(LOG_TAG, "deleteLoggedHour success");
             Log.e(LOG_TAG, " successSubmissionChecker : " + submissionChecker.isSuccessful());
             deleteCallback.onDeleteSuccess();
         }

         @Override
         public void failure(RetrofitError error) {
             Log.e(LOG_TAG, "deleteLoggedHour failure");
             Log.e(LOG_TAG, " Error : ", error);
             deleteCallback.onDeleteFailure();
         }
     });
}

Finally the error shows this one

retrofit.RetrofitError: TimeTrackerWebServiceMethods.deleteLoggedHour: @Body parameters cannot be used with form or multi-part encoding. (parameter #1)
        at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:400)
        at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
        at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
        at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at retrofit.Platform$Android$2$1.run(Platform.java:142)
        at java.lang.Thread.run(Thread.java:856)
 Caused by: java.lang.IllegalArgumentException: TimeTrackerWebServiceMethods.deleteLoggedHour: @Body parameters cannot be used with form or multi-part encoding. (parameter #1)
        at retrofit.RestMethodInfo.methodError(RestMethodInfo.java:107)
        at retrofit.RestMethodInfo.parameterError(RestMethodInfo.java:111)
        at retrofit.RestMethodInfo.parseParameters(RestMethodInfo.java:387)
        at retrofit.RestMethodInfo.init(RestMethodInfo.java:118)
        at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:294)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220) 
at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
            at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at retrofit.Platform$Android$2$1.run(Platform.java:142)
            at java.lang.Thread.run(Thread.java:856)

Am I doing something wrong?

Paula Kristin
  • 813
  • 2
  • 11
  • 27
  • `@FormUrlEncoded` can't have a `@Body`. Either remove the `@FormUrlEncoded`, or define your parameters as `@Query` (the test and the exception are here: https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit/RequestFactoryParser.java#L323 ) – njzk2 Jul 03 '15 at 14:44
  • got it thanks. I used it and it works! – Paula Kristin Jul 03 '15 at 17:47
  • Probably who visit here, they can check this answer. https://stackoverflow.com/questions/41509195/how-to-send-a-http-delete-with-a-body-in-retrofit – Ritesh Adulkar Nov 11 '20 at 05:36

1 Answers1

3

You may right that the OkHttp and Retrofit does not support sending a body in a DELETE request.

This is OK since it makes no sense to send a body with a DELETE request. And also Http spec does not explicitly forbid or discourage it. See this question: Is an entity body allowed for an HTTP DELETE request?

Bodies on DELETE requests have no defined semantics. Note that
sending a body on a DELETE request might cause some existing
implementations to reject the request.

Community
  • 1
  • 1
bhdrkn
  • 6,244
  • 5
  • 35
  • 42