Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wd1105040417
retrofit
提交
4c381475
R
retrofit
项目概览
wd1105040417
/
retrofit
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
retrofit
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4c381475
编写于
11月 22, 2014
作者:
D
Dima Gerasimov
提交者:
Jake Wharton
11月 23, 2014
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Optional @Field/@FieldMap URL-encoding.
上级
9b0cc00c
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
91 addition
and
7 deletion
+91
-7
retrofit/src/main/java/retrofit/RequestBuilder.java
retrofit/src/main/java/retrofit/RequestBuilder.java
+12
-5
retrofit/src/main/java/retrofit/http/Field.java
retrofit/src/main/java/retrofit/http/Field.java
+6
-0
retrofit/src/main/java/retrofit/http/FieldMap.java
retrofit/src/main/java/retrofit/http/FieldMap.java
+5
-0
retrofit/src/main/java/retrofit/mime/FormUrlEncodedTypedOutput.java
...rc/main/java/retrofit/mime/FormUrlEncodedTypedOutput.java
+10
-2
retrofit/src/test/java/retrofit/RequestBuilderTest.java
retrofit/src/test/java/retrofit/RequestBuilderTest.java
+58
-0
未找到文件。
retrofit/src/main/java/retrofit/RequestBuilder.java
浏览文件 @
4c381475
...
...
@@ -272,26 +272,32 @@ final class RequestBuilder implements RequestInterceptor.RequestFacade {
}
}
else
if
(
annotationType
==
Field
.
class
)
{
if
(
value
!=
null
)
{
// Skip null values.
String
name
=
((
Field
)
annotation
).
value
();
Field
field
=
(
Field
)
annotation
;
String
name
=
field
.
value
();
boolean
encodeName
=
field
.
encodeName
();
boolean
encodeValue
=
field
.
encodeValue
();
if
(
value
instanceof
Iterable
)
{
for
(
Object
iterableValue
:
(
Iterable
<?>)
value
)
{
if
(
iterableValue
!=
null
)
{
// Skip null values.
formBody
.
addField
(
name
,
iterableValue
.
toString
()
);
formBody
.
addField
(
name
,
encodeName
,
iterableValue
.
toString
(),
encodeValue
);
}
}
}
else
if
(
value
.
getClass
().
isArray
())
{
for
(
int
x
=
0
,
arrayLength
=
Array
.
getLength
(
value
);
x
<
arrayLength
;
x
++)
{
Object
arrayValue
=
Array
.
get
(
value
,
x
);
if
(
arrayValue
!=
null
)
{
// Skip null values.
formBody
.
addField
(
name
,
arrayValue
.
toString
()
);
formBody
.
addField
(
name
,
encodeName
,
arrayValue
.
toString
(),
encodeValue
);
}
}
}
else
{
formBody
.
addField
(
name
,
value
.
toString
()
);
formBody
.
addField
(
name
,
encodeName
,
value
.
toString
(),
encodeValue
);
}
}
}
else
if
(
annotationType
==
FieldMap
.
class
)
{
if
(
value
!=
null
)
{
// Skip null values.
FieldMap
fieldMap
=
(
FieldMap
)
annotation
;
boolean
encodeNames
=
fieldMap
.
encodeNames
();
boolean
encodeValues
=
fieldMap
.
encodeValues
();
for
(
Map
.
Entry
<?,
?>
entry
:
((
Map
<?,
?>)
value
).
entrySet
())
{
Object
entryKey
=
entry
.
getKey
();
if
(
entryKey
==
null
)
{
...
...
@@ -300,7 +306,8 @@ final class RequestBuilder implements RequestInterceptor.RequestFacade {
}
Object
entryValue
=
entry
.
getValue
();
if
(
entryValue
!=
null
)
{
// Skip null values.
formBody
.
addField
(
entryKey
.
toString
(),
entryValue
.
toString
());
formBody
.
addField
(
entryKey
.
toString
(),
encodeNames
,
entryValue
.
toString
(),
encodeValues
);
}
}
}
...
...
retrofit/src/main/java/retrofit/http/Field.java
浏览文件 @
4c381475
...
...
@@ -56,4 +56,10 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Retention
(
RUNTIME
)
public
@interface
Field
{
String
value
();
/** Specifies whether {@link #value()} is URL encoded. */
boolean
encodeName
()
default
true
;
/** Specifies whether the argument value to the annotated method parameter is URL encoded. */
boolean
encodeValue
()
default
true
;
}
retrofit/src/main/java/retrofit/http/FieldMap.java
浏览文件 @
4c381475
...
...
@@ -44,4 +44,9 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Target
(
PARAMETER
)
@Retention
(
RUNTIME
)
public
@interface
FieldMap
{
/** Specifies whether parameter names (keys in the map) are URL encoded. */
boolean
encodeNames
()
default
true
;
/** Specifies whether parameter values (values in the map) are URL encoded. */
boolean
encodeValues
()
default
true
;
}
retrofit/src/main/java/retrofit/mime/FormUrlEncodedTypedOutput.java
浏览文件 @
4c381475
...
...
@@ -24,6 +24,10 @@ public final class FormUrlEncodedTypedOutput implements TypedOutput {
final
ByteArrayOutputStream
content
=
new
ByteArrayOutputStream
();
public
void
addField
(
String
name
,
String
value
)
{
addField
(
name
,
true
,
value
,
true
);
}
public
void
addField
(
String
name
,
boolean
encodeName
,
String
value
,
boolean
encodeValue
)
{
if
(
name
==
null
)
{
throw
new
NullPointerException
(
"name"
);
}
...
...
@@ -34,8 +38,12 @@ public final class FormUrlEncodedTypedOutput implements TypedOutput {
content
.
write
(
'&'
);
}
try
{
name
=
URLEncoder
.
encode
(
name
,
"UTF-8"
);
value
=
URLEncoder
.
encode
(
value
,
"UTF-8"
);
if
(
encodeName
)
{
name
=
URLEncoder
.
encode
(
name
,
"UTF-8"
);
}
if
(
encodeValue
)
{
value
=
URLEncoder
.
encode
(
value
,
"UTF-8"
);
}
content
.
write
(
name
.
getBytes
(
"UTF-8"
));
content
.
write
(
'='
);
...
...
retrofit/src/test/java/retrofit/RequestBuilderTest.java
浏览文件 @
4c381475
...
...
@@ -1456,6 +1456,30 @@ public class RequestBuilderTest {
assertTypedBytes
(
request
.
getBody
(),
"foo=bar&ping=pong"
);
}
@Test
public
void
formEncodedWithEncodedNameFieldParam
()
{
class
Example
{
@FormUrlEncoded
//
@POST
(
"/foo"
)
//
Response
method
(
@Field
(
value
=
"na+me"
,
encodeName
=
false
)
String
foo
)
{
return
null
;
}
}
Request
request
=
buildRequest
(
Example
.
class
,
"ba r"
);
assertTypedBytes
(
request
.
getBody
(),
"na+me=ba+r"
);
}
@Test
public
void
formEncodedWithEncodedValueFieldParam
()
{
class
Example
{
@FormUrlEncoded
//
@POST
(
"/foo"
)
//
Response
method
(
@Field
(
value
=
"na me"
,
encodeValue
=
false
)
String
foo
)
{
return
null
;
}
}
Request
request
=
buildRequest
(
Example
.
class
,
"ba+r"
);
assertTypedBytes
(
request
.
getBody
(),
"na+me=ba+r"
);
}
@Test
public
void
formEncodedFieldOptional
()
{
class
Example
{
@FormUrlEncoded
//
...
...
@@ -1511,6 +1535,40 @@ public class RequestBuilderTest {
assertTypedBytes
(
request
.
getBody
(),
"foo=1&foo=2&foo=3&kit=kat"
);
}
@Test
public
void
formEncodedWithEncodedNameFieldParamMap
()
{
class
Example
{
@FormUrlEncoded
//
@POST
(
"/foo"
)
//
Response
method
(
@FieldMap
(
encodeNames
=
false
)
Map
<
String
,
Object
>
fieldMap
)
{
return
null
;
}
}
Map
<
String
,
Object
>
fieldMap
=
new
LinkedHashMap
<
String
,
Object
>();
fieldMap
.
put
(
"k+it"
,
"k at"
);
fieldMap
.
put
(
"pin+g"
,
"po ng"
);
Request
request
=
buildRequest
(
Example
.
class
,
fieldMap
);
assertTypedBytes
(
request
.
getBody
(),
"k+it=k+at&pin+g=po+ng"
);
}
@Test
public
void
formEncodedWithEncodedValueFieldParamMap
()
{
class
Example
{
@FormUrlEncoded
//
@POST
(
"/foo"
)
//
Response
method
(
@FieldMap
(
encodeValues
=
false
)
Map
<
String
,
Object
>
fieldMap
)
{
return
null
;
}
}
Map
<
String
,
Object
>
fieldMap
=
new
LinkedHashMap
<
String
,
Object
>();
fieldMap
.
put
(
"k it"
,
"k+at"
);
fieldMap
.
put
(
"pin g"
,
"po+ng"
);
Request
request
=
buildRequest
(
Example
.
class
,
fieldMap
);
assertTypedBytes
(
request
.
getBody
(),
"k+it=k+at&pin+g=po+ng"
);
}
@Test
public
void
formEncodedFieldMap
()
{
class
Example
{
@FormUrlEncoded
//
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录