2015-07-03 55 views
1

我已經嘗試了一切,但它仍然無法正常工作。我已經使用okhttp 2.0和okhttp-urlconnection 2.0加上1.9的改造。刪除身體仍然不起作用,我已經檢查了之前發佈的所有問題,但我仍然無法找到答案。有什麼我做錯了嗎?刪除與身體仍然無法與改造

這是我的接口是什麼樣子:

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

爲我WebSevice,它看起來像這樣:

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; 
} 

這就是我所說的方法:

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(); 
     } 
    }); 
} 

最後錯誤顯示這一個

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) 

我做錯了什麼?

+0

'@ FormUrlEncoded'不能有'@ Body'。要麼刪除'@ FormUrlEncoded',要麼將參數定義爲'@ Query'(測試和例外在這裏:https://github.com/square/retrofit/blob/master/retrofit/src/main/java/翻新/ RequestFactoryParser.java#L323) – njzk2

+0

得到它的感謝。我用它,它的工作原理! –

回答

3

您可能會錯,OkHttpRetrofit不支持在DELETE請求中發送主體。

這是好的,因爲發送帶有DELETE請求的主體沒有意義。並且Http spec也沒有明確地禁止或阻止它。看到這個問題:Is an entity body allowed for an HTTP DELETE request

DELETE請求上的實體沒有定義的語義。請注意,
通過DELETE請求發送主體可能會導致一些現有的實現拒絕該請求。