Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wd1105040417
retrofit
提交
ab26b8b0
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,发现更多精彩内容 >>
提交
ab26b8b0
编写于
12月 05, 2010
作者:
E
Eric Burke
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
introduced the ClientMessage class so client errors can include title, message, and button text.
上级
1eab56aa
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
80 addition
and
3 deletion
+80
-3
modules/core/src/retrofit/core/Callback.java
modules/core/src/retrofit/core/Callback.java
+1
-1
modules/core/src/retrofit/core/ClientMessage.java
modules/core/src/retrofit/core/ClientMessage.java
+43
-0
modules/http/src/retrofit/http/CallbackResponseHandler.java
modules/http/src/retrofit/http/CallbackResponseHandler.java
+34
-1
modules/http/src/retrofit/http/UiCallback.java
modules/http/src/retrofit/http/UiCallback.java
+2
-1
未找到文件。
modules/core/src/retrofit/core/Callback.java
浏览文件 @
ab26b8b0
...
...
@@ -35,7 +35,7 @@ public interface Callback<T> {
*
* @param message to show user, or null if no message was returned
*/
void
clientError
(
String
message
);
void
clientError
(
ClientMessage
message
);
/**
* We reached the server, but it encountered an error. Please try again
...
...
modules/core/src/retrofit/core/ClientMessage.java
0 → 100644
浏览文件 @
ab26b8b0
// Copyright 2010 Square, Inc.
package
retrofit.core
;
/**
* Information for a client error screen or dialog, including the screen title,
* message, and button label. Any field may be null.
*
* @author Eric Burke (eric@squareup.com)
*/
public
class
ClientMessage
{
private
final
String
title
;
private
final
String
message
;
private
final
String
buttonLabel
;
/**
* Constructs a new message, any argument may be null.
*
* @param title a few words for a dialog title or heading, or null.
* @param message a sentence or two with a more detailed, user friendly
* message, or null.
* @param buttonLabel text to display on a button, or null.
*/
public
ClientMessage
(
String
title
,
String
message
,
String
buttonLabel
)
{
this
.
title
=
title
;
this
.
message
=
message
;
this
.
buttonLabel
=
buttonLabel
;
}
/** Returns a few words useful for a dialog title or heading, might be null. */
public
String
getTitle
()
{
return
title
;
}
/** Returns a sentence or two with a user friendly message. */
public
String
getMessage
()
{
return
message
;
}
/** Returns text for a button, or null. */
public
String
getButtonLabel
()
{
return
buttonLabel
;
}
}
modules/http/src/retrofit/http/CallbackResponseHandler.java
浏览文件 @
ab26b8b0
...
...
@@ -7,6 +7,7 @@ import org.apache.http.HttpResponse;
import
org.apache.http.StatusLine
;
import
org.apache.http.client.ResponseHandler
;
import
retrofit.core.Callback
;
import
retrofit.core.ClientMessage
;
import
java.io.IOException
;
import
java.util.logging.Level
;
...
...
@@ -114,7 +115,7 @@ public abstract class CallbackResponseHandler<T>
String
body
=
new
String
(
HttpClients
.
entityToBytes
(
entity
),
"UTF-8"
);
logger
.
fine
(
"Server returned "
+
statusCode
+
", "
+
statusLine
.
getReasonPhrase
()
+
". Body: "
+
body
);
callback
.
clientError
(
parse
ServerMessage
(
statusCode
,
body
));
callback
.
clientError
(
parse
ClientMessage
(
body
));
}
else
{
logger
.
fine
(
"Server returned "
+
statusCode
+
", "
+
statusLine
.
getReasonPhrase
()
+
"."
);
...
...
@@ -123,6 +124,29 @@ public abstract class CallbackResponseHandler<T>
return
null
;
}
/**
* Parses a client error message, assuming the JSON looks like this:
* <pre>
* {
* "message_title": "Email Address Taken",
* "message": "That email is already taken. Please enter another address.",
* "button_label": "Change"
* }</pre>
*/
private
static
ClientMessage
parseClientMessage
(
String
body
)
{
try
{
ClientError
error
=
new
Gson
().
fromJson
(
body
,
ClientError
.
class
);
if
(
error
!=
null
)
{
return
new
ClientMessage
(
error
.
message_title
,
error
.
message
,
error
.
button_label
);
}
}
catch
(
Throwable
t
)
{
// The server error takes precedence.
logger
.
log
(
Level
.
WARNING
,
t
.
getMessage
(),
t
);
}
return
null
;
}
/**
* Parses a server error message.
*/
...
...
@@ -146,4 +170,13 @@ public abstract class CallbackResponseHandler<T>
static
class
ServerError
{
String
message
;
}
/**
* Gson POJO for parsing client error messages.
*/
static
class
ClientError
{
String
message
;
String
message_title
;
String
button_label
;
}
}
modules/http/src/retrofit/http/UiCallback.java
浏览文件 @
ab26b8b0
...
...
@@ -2,6 +2,7 @@
package
retrofit.http
;
import
retrofit.core.Callback
;
import
retrofit.core.ClientMessage
;
import
retrofit.core.MainThread
;
/**
...
...
@@ -48,7 +49,7 @@ public final class UiCallback<T> implements Callback<T> {
});
}
public
void
clientError
(
final
String
message
)
{
public
void
clientError
(
final
ClientMessage
message
)
{
mainThread
.
execute
(
new
Runnable
()
{
public
void
run
()
{
delegate
.
clientError
(
message
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录