回答編集履歴

1

Moshiを使うように修正

2019/04/29 00:10

投稿

退会済みユーザー
test CHANGED
@@ -1 +1,85 @@
1
- Gsonを使いました。
1
+ `@JsonClass`アノテーション追加しました。
2
+
3
+
4
+
5
+ ```
6
+
7
+ @JsonClass(generateAdapter = true)
8
+
9
+ data class GitHubUser(
10
+
11
+ val name: String,
12
+
13
+ val id: String,
14
+
15
+ @Json(name = "avatar_url")
16
+
17
+ val avatarUrl: String
18
+
19
+ )
20
+
21
+ ```
22
+
23
+
24
+
25
+ このアノテーションを使うために、Moshi本体とcodegenを追加しました。
26
+
27
+
28
+
29
+ ```
30
+
31
+ dependencies {
32
+
33
+ // Retrofit2
34
+
35
+ implementation "com.squareup.retrofit2:retrofit:${RetrofitVersion}"
36
+
37
+ implementation "com.squareup.retrofit2:converter-moshi:${RetrofitVersion}"
38
+
39
+
40
+
41
+ // Moshi
42
+
43
+ implementation "com.squareup.moshi:moshi:${MoshiVersion}"
44
+
45
+ kapt "com.squareup.moshi:moshi-kotlin-codegen:${MoshiVersion}"
46
+
47
+ }
48
+
49
+ ```
50
+
51
+
52
+
53
+ ```
54
+
55
+ interface GitHubApi {
56
+
57
+ companion object {
58
+
59
+ val instance: GitHubApi = Retrofit.Builder()
60
+
61
+ .baseUrl(BuildConfig.BASE_URL)
62
+
63
+ .addConverterFactory(MoshiConverterFactory.create(Moshi.Builder().build()))
64
+
65
+ .addCallAdapterFactory(CoroutineCallAdapterFactory())
66
+
67
+ .build()
68
+
69
+ .create(GitHubApi::class.java)
70
+
71
+ }
72
+
73
+
74
+
75
+ @GET("users/{user_name}")
76
+
77
+ fun fetchUserAsync(@Path("user_name") userName: String): Deferred<GitHubUser>
78
+
79
+ }
80
+
81
+ ```
82
+
83
+
84
+
85
+ [Convert Snakecase to Camelcase with Moshi](https://okuzawats.com/snakecase-to-camelcase-with-moshi/)