使用 Retrofit 创建自定义 DELETE 方法

Create custom DELETE method with Retrofit

我在对正文执行删除请求时遇到问题。我正在使用带有 OkHttp 2.2 的 Retrofit 1.9。

所以我找到了一个解决方案 here 其他人给出了相同的解决方案,即创建一个像这样的自定义删除方法:

@Target(METHOD)
@Retention(RUNTIME)
@RestMethod(hasBody = true, value = "DELETE")
public @interface CustomDelete {
    String value();
}

我在我的 API 界面中添加了这段代码(使用所有方法来请求我的 API)。

但是Android Studio 无法解析符号METHOD 和RUNTIME。我不知道要包含什么才能使用它。

如果有人能帮我弄清楚,有解释就更好了。 与此同时,我一直在网上寻找解决方案(然后在这里分享)。

@Target 和@Retention 在 Android SDK 中声明,在 java.lang.annotation

public @interface Target {
    /**
     * Returns an array of the kinds of elements an annotation type
     * can be applied to.
     * @return an array of the kinds of elements an annotation type
     * can be applied to
     */
    ElementType[] value();
}


public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}

从源代码来看,@Target 接受 ElementTypes,@Retention 接受 RetentionPolicy。

https://developer.android.com/reference/java/lang/annotation/ElementType.html https://developer.android.com/reference/java/lang/annotation/RetentionPolicy.html

试试这个

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)