异步FormUrlEn codeD删除通话改造抛出抛出:IllegalArgumentException异常 [英] Retrofit throwing IllegalArgumentException exception for asynchronous FormUrlEncoded DELETE call

查看:464
本文介绍了异步FormUrlEn codeD删除通话改造抛出抛出:IllegalArgumentException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个异步POST和DELETE这是形式的URL连接codeD的Andr​​oid 4.4系统的使用改装

I'm trying to make an asynchronous POST and DELETE which is form url encoded using Retrofit in Android 4.4

下面是我的客户 -

Here is my client -

@FormUrlEncoded
@POST(INetwork.API_BASE_PREFIX + "/memberships.json")
void join(@Field("id") String id, Callback<?> cb);

@FormUrlEncoded
@DELETE(INetwork.API_BASE_PREFIX + "/memberships.json")
void leave(@Field("id") String id, Callback<?> cb);

这是个例外 -

And this is the exception -

java.lang.IllegalArgumentException: IRepositoryClient.leave: FormUrlEncoded can only be specified on HTTP methods with request body (e.g., @POST).
        at retrofit.RestMethodInfo.methodError(RestMethodInfo.java:118)
        at retrofit.RestMethodInfo.parseMethodAnnotations(RestMethodInfo.java:191)
        at retrofit.RestMethodInfo.init(RestMethodInfo.java:128)
        at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:329)
        at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:264)
        at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:315)
        at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at retrofit.Platform$Android$2$1.run(Platform.java:142)
        at java.lang.Thread.run(Thread.java:841)

我看了看,通过源和它基本上如果方法不具有主体和请求formurlen codeD,这个异常。另外我注意到FormUrlEn codeD做工精细的所有例子,当它不同步,即我有某种没有回调的返回类型 - 对不起,我有点失落

I looked through the source and it basically if the method doesn't have a body and the request is formurlencoded, this exception is thrown. Also I noticed that all example of FormUrlEncoded work fine when it is not asynchronous, i.e. I have a return type of some sort and no callback - sorry I'm a little lost

如果我发送一个空的身体吗?我是否需要发送一个,也不会在@Field参数就够了?

Should I send an empty body? Am I required to send one and won't the @Field parameters suffice?

使用改装1.5.0

推荐答案

在RFC的HTTP是不清楚DELETE方法是否被允许有一个请求主体与否。改造上没有一个侧面犯错的。

The RFC for HTTP is unclear on whether or not the DELETE method is allowed to have a request body or not. Retrofit err's on the side of not having one.

不过,你仍然可以包含一个(假定HTTP客户端支持的话)通过使用自定义HTTP方法注释。

However, you can still include one (assuming the HTTP client supports it) by using a custom HTTP method annotation.

package com.myapp;

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

现在具体的使用定义的自定义注解的接口方法。

Now specific your interface method using the custom annotation you defined.

@FormUrlEncoded
@BODY_DELETE(INetwork.API_BASE_PREFIX + "/memberships.json")
void leave(@Field("id") String id, Callback<?> cb);

这篇关于异步FormUrlEn codeD删除通话改造抛出抛出:IllegalArgumentException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆