質問編集履歴
2
誤字修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -28,7 +28,7 @@
|
|
28
28
|
Response<Menber> resPostMenber = postMenber(fragment.sessionManager.getJwtToken(),
|
29
29
|
family_id,
|
30
30
|
member_id,
|
31
|
-
|
31
|
+
requestMenber)
|
32
32
|
.execute();
|
33
33
|
```
|
34
34
|
|
1
内容詳細追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,63 +1,39 @@
|
|
1
|
+
## はじめに
|
1
2
|
retrofitを最近触り始めたのですが、
|
2
3
|
エラーハンドリングがうまくできず…
|
3
4
|
皆さんのお力を貸していただければ幸いです。
|
4
5
|
|
5
|
-
|
6
|
+
## 内容
|
6
|
-
|
7
|
+
###やりたいこと
|
7
|
-
バックエンドからのレスポンスとして201のステータスコードを返しているが、
|
8
|
-
Client側(Android)で「200」のステータスが返されてしまう。
|
9
|
-
|
10
|
-
**
|
8
|
+
**エラーレスポンスが返ってきた際も201などと同様に、「null」ではなく、レスポンスとして返した内容(Object)を埋め込みたい**
|
9
|
+
### 現状
|
11
|
-
|
10
|
+
バックエンドからエラーステータス(今回は「409」)を返しており、
|
12
|
-
Client側でも正しく受けとるが、
|
11
|
+
Client側でもエラーステータスを正しく受けとるが、レスポンスボディに「null」が埋め込まれている(Response.java)。
|
13
|
-
※rawResponse,
|
12
|
+
※rawResponse, body, errorBody)の中の”body”が該当
|
14
13
|
・rawResponseはここでは気にしない
|
15
14
|
・errorBodyは正しいものが表示される
|
16
15
|
|
17
|
-
※
|
18
|
-
https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit2/Response.java
|
16
|
+
※[retrofit2公式Github](https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit2/Response.java):https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit2/Response.java
|
19
17
|
|
20
|
-
|
18
|
+
下記↓今回使用したInterface.java
|
21
|
-
```
|
19
|
+
```Interface.java
|
22
|
-
public static <T> Response<T> success(@Nullable T body) {
|
23
|
-
return success(body, new okhttp3.Response.Builder() //
|
24
|
-
.code(200)
|
25
|
-
.message("OK")
|
26
|
-
.protocol(Protocol.HTTP_1_1)
|
27
|
-
|
20
|
+
@POST("/api/family/{family_id}/menber/{member_id})
|
28
|
-
.build());
|
29
|
-
}
|
30
|
-
|
31
|
-
・・・
|
32
|
-
|
33
|
-
public static <T> Response<T> error(int code, ResponseBody body) {
|
34
|
-
|
21
|
+
Observable<Menber> postMenber(@Header("Authorization") String token,
|
35
|
-
return error(body, new okhttp3.Response.Builder() //
|
36
|
-
.code(code)
|
37
|
-
.message("Response.error()")
|
38
|
-
.protocol(Protocol.HTTP_1_1)
|
39
|
-
.request(new Request.Builder().url("http://localhost/").build())
|
40
|
-
.build());
|
41
|
-
}
|
42
|
-
|
43
|
-
public static <T> Response<T> error(ResponseBody body, okhttp3.Response rawResponse) {
|
44
|
-
checkNotNull(body, "body == null");
|
45
|
-
|
22
|
+
@Path(value = "family_id") String FamilyID,
|
46
|
-
if (rawResponse.isSuccessful()) {
|
47
|
-
throw new IllegalArgumentException("rawResponse should not be successful response");
|
48
|
-
}
|
49
|
-
|
23
|
+
@Path(value = "member_id") String MenberID,
|
50
|
-
}
|
51
|
-
|
52
|
-
|
24
|
+
@Body Member member);
|
53
25
|
```
|
26
|
+
下記↓上記Interface使用箇所
|
27
|
+
```Activity.java
|
28
|
+
Response<Menber> resPostMenber = postMenber(fragment.sessionManager.getJwtToken(),
|
29
|
+
family_id,
|
30
|
+
member_id,
|
31
|
+
requestRFID)
|
32
|
+
.execute();
|
33
|
+
```
|
54
34
|
|
55
|
-
|
56
|
-
|
57
35
|
おそらく、retrofit2の下記仕様により、現在の事象が起きてしまっている(?)と思われるのですが
|
58
36
|
なにか対応できる方法がないか探しています。
|
37
|
+
- status >= 400の場合、一律bodyをnullで返す
|
59
38
|
|
60
|
-
・successStatusの場合、一律で200を返す
|
61
|
-
・status >= 400の場合、一律bodyをnullで返す
|
62
|
-
|
63
39
|
もしご存知であればご教示いただけますと幸いです。
|