Retrofit 2.7.2使用retrofit调用时发生GSON错误,错误为“无法调用类的无参数构造函数“,无法找到解决方案

kb5ga3dv  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(117)

我正在使用android studio和Kotlin,试图使用gson库进行API调用。我使用JSON到kotlin数据类插件来制作我的数据类。我已经寻找了解决方案,但还没有找到任何与我的问题完全匹配的东西。我寻找了如何在kotlin中添加一个无参数构造函数,但这涉及到将所有字段设置为默认值,这不是我想要的。我想知道它是否在调用API,但没有收到任何转换率,但我不知道如何检查,因为我立即得到抛出调用API时的错误。
我收到的确切错误是:
“无法为类com.schrader.currency_take_two.data.models.ConversionRates调用无参数构造函数。使用此类型的Gson注册InstanceCreator可能会解决此问题。”
这是我的身材

  1. plugins {
  2. id 'com.android.application'
  3. id 'org.jetbrains.kotlin.android'
  4. id 'dagger.hilt.android.plugin'
  5. id 'kotlin-kapt'
  6. }
  7. android {
  8. compileSdk 31
  9. defaultConfig {
  10. applicationId "com.schrader.currency_take_two"
  11. minSdk 21
  12. targetSdk 31
  13. versionCode 1
  14. versionName "1.0"
  15. testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  16. }
  17. buildTypes {
  18. release {
  19. minifyEnabled false
  20. proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  21. }
  22. }
  23. compileOptions {
  24. sourceCompatibility JavaVersion.VERSION_1_8
  25. targetCompatibility JavaVersion.VERSION_1_8
  26. }
  27. kotlinOptions {
  28. jvmTarget = '1.8'
  29. }
  30. buildFeatures{
  31. viewBinding true
  32. }
  33. }
  34. dependencies {
  35. implementation 'androidx.core:core-ktx:1.7.0'
  36. implementation 'androidx.appcompat:appcompat:1.4.1'
  37. implementation 'com.google.android.material:material:1.5.0'
  38. implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
  39. testImplementation 'junit:junit:4.13.2'
  40. androidTestImplementation 'androidx.test.ext:junit:1.1.3'
  41. androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
  42. // dagger-hilt
  43. implementation 'com.google.dagger:hilt-android:2.41'
  44. kapt 'com.google.dagger:hilt-compiler:2.41'
  45. // retrofit
  46. implementation 'com.squareup.retrofit2:retrofit:2.7.2'
  47. implementation 'com.squareup.retrofit2:converter-gson:2.7.2'
  48. implementation 'com.squareup.okhttp3:okhttp:3.6.0'
  49. // coroutines
  50. implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
  51. // ViewModel
  52. def lifecycle_version = "2.4.1"
  53. implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
  54. // LiveData
  55. implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
  56. // Lifecycles only (without ViewModel or LiveData)
  57. implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
  58. implementation "androidx.core:core-ktx:1.7.0"
  59. //fragment KTX library
  60. implementation "androidx.fragment:fragment-ktx:1.4.1"
  61. implementation "androidx.activity:activity-ktx:1.4.0"
  62. // Room DB
  63. def room_version = "2.4.2"
  64. implementation "androidx.room:room-runtime:$room_version"
  65. kapt "androidx.room:room-compiler:$room_version"
  66. }

以下是数据类,其中一个在这里被缩短,因为它很长:

  1. package com.schrader.currency_take_two.data.models
  2. import com.google.gson.annotations.SerializedName
  3. data class CurrencyResponse(
  4. @SerializedName("base_code")
  5. val baseCode: String,
  6. @SerializedName("conversion_rates")
  7. val conversionRates: ConversionRates,
  8. @SerializedName("documentation")
  9. val documentation: String,
  10. @SerializedName("result")
  11. val result: String,
  12. @SerializedName("terms_of_use")
  13. val termsOfUse: String,
  14. @SerializedName("time_last_update_unix")
  15. val timeLastUpdateUnix: Int,
  16. @SerializedName("time_last_update_utc")
  17. val timeLastUpdateUtc: String,
  18. @SerializedName("time_next_update_unix")
  19. val timeNextUpdateUnix: Int,
  20. @SerializedName("time_next_update_utc")
  21. val timeNextUpdateUtc: String
  22. )
  23. package com.schrader.currency_take_two.data.models
  24. import com.google.gson.annotations.SerializedName
  25. data class ConversionRates(
  26. @SerializedName("AED")
  27. val aED: Double,
  28. @SerializedName("AFN")
  29. val aFN: Double,
  30. @SerializedName("ALL")
  31. val aLL: Double,
  32. @SerializedName("AMD")
  33. val aMD: Double,
  34. @SerializedName("ANG")
  35. val aNG: Double,
  36. @SerializedName("AOA")
  37. val aOA: Double,
  38. @SerializedName("ARS")
  39. val aRS: Double
  40. )

下面是改造调用和我的改造示例:

  1. interface CurrencyApi {
  2. @GET("/v6/API_KEY/latest/{Currency}")
  3. suspend fun getRates(
  4. @Path("Currency") currency: String //This adds onto the API URL in the {} in the GET, @Query adds a query to the URL 'Query={Query}'
  5. ): Response<CurrencyResponse>
  6. }

API调用没有到达第一个Log调用,并且立即错误出到catch块。
第一个
下面是我在主Activity中调用API的方式:

  1. GlobalScope.launch(Dispatchers.IO){
  2. var response = apiRepo.getRates("USD")
  3. if(response.data == null){
  4. Log.d("MAIN", "${response.message}")
  5. }
  6. Log.d("MAIN", "${response.toString()}")
  7. }

下面是来自API的确切JSON:

  1. {
  2. "result":"success",
  3. "documentation":"https://www.exchangerate-api.com/docs",
  4. "terms_of_use":"https://www.exchangerate-api.com/terms",
  5. "time_last_update_unix":1649203202,
  6. "time_last_update_utc":"Wed, 06 Apr 2022 00:00:02 +0000",
  7. "time_next_update_unix":1649289602,
  8. "time_next_update_utc":"Thu, 07 Apr 2022 00:00:02 +0000",
  9. "base_code":"USD",
  10. "conversion_rates":{
  11. "USD":1,
  12. "AED":3.6725,
  13. "AFN":88.6534,
  14. "ALL":110.0838,
  15. "AMD":480.1007,
  16. "ANG":1.7900,
  17. "AOA":445.0124,
  18. "ARS":111.4453,
  19. "AUD":1.3155,
  20. "AWG":1.7900,
  21. "AZN":1.6961,
  22. "BAM":1.7892,
  23. "BBD":2.0000,
  24. "BDT":85.2904,
  25. "BGN":1.7884,
  26. "BHD":0.3760,
  27. "BIF":2005.2598,
  28. "BMD":1.0000,
  29. "BND":1.3581,
  30. "BOB":6.8635,
  31. "BRL":4.6151,
  32. "BSD":1.0000,
  33. "BTN":75.3175,
  34. "BWP":11.5278,
  35. "BYN":2.8692,
  36. "BZD":2.0000,
  37. "CAD":1.2468,
  38. "CDF":1995.8399,
  39. "CHF":0.9283,
  40. "CLP":779.3730,
  41. "CNY":6.3692,
  42. "COP":3716.7222,
  43. "CRC":660.0122,
  44. "CUP":24.0000,
  45. "CVE":100.8701,
  46. "CZK":22.2978,
  47. "DJF":177.7210,
  48. "DKK":6.8247,
  49. "DOP":54.9168,
  50. "DZD":143.1850,
  51. "EGP":18.2463,
  52. "ERN":15.0000,
  53. "ETB":51.2293,
  54. "EUR":0.9148,
  55. "FJD":2.0753,
  56. "FKP":0.7634,
  57. "FOK":6.8247,
  58. "GBP":0.7634,
  59. "GEL":3.0853,
  60. "GGP":0.7634,
  61. "GHS":7.7634,
  62. "GIP":0.7634,
  63. "GMD":54.8016,
  64. "GNF":8946.2447,
  65. "GTQ":7.6691,
  66. "GYD":209.6041,
  67. "HKD":7.8373,
  68. "HNL":24.5487,
  69. "HRK":6.8925,
  70. "HTG":107.4322,
  71. "HUF":341.7488,
  72. "IDR":14374.8444,
  73. "ILS":3.2117,
  74. "IMP":0.7634,
  75. "INR":75.3200,
  76. "IQD":1462.3970,
  77. "IRR":41938.4058,
  78. "ISK":129.1085,
  79. "JEP":0.7634,
  80. "JMD":153.5796,
  81. "JOD":0.7090,
  82. "JPY":123.3508,
  83. "KES":115.6196,
  84. "KGS":85.8864,
  85. "KHR":4057.0179,
  86. "KID":1.3156,
  87. "KMF":450.0508,
  88. "KRW":1215.3051,
  89. "KWD":0.2996,
  90. "KYD":0.8333,
  91. "KZT":466.7579,
  92. "LAK":13220.0876,
  93. "LBP":1507.5000,
  94. "LKR":295.1163,
  95. "LRD":152.7083,
  96. "LSL":14.6202,
  97. "LYD":4.6812,
  98. "MAD":9.4783,
  99. "MDL":18.3389,
  100. "MGA":3035.6361,
  101. "MKD":56.0557,
  102. "MMK":1825.6103,
  103. "MNT":2954.7230,
  104. "MOP":8.0723,
  105. "MRU":36.4708,
  106. "MUR":44.3148,
  107. "MVR":15.4209,
  108. "MWK":820.4555,
  109. "MXN":19.9358,
  110. "MYR":4.2051,
  111. "MZN":64.2611,
  112. "NAD":14.6202,
  113. "NGN":415.4445,
  114. "NIO":35.8669,
  115. "NOK":8.7398,
  116. "NPR":120.5080,
  117. "NZD":1.4357,
  118. "OMR":0.3845,
  119. "PAB":1.0000,
  120. "PEN":3.6681,
  121. "PGK":3.5228,
  122. "PHP":51.2574,
  123. "PKR":183.8781,
  124. "PLN":4.2286,
  125. "PYG":7064.7743,
  126. "QAR":3.6400,
  127. "RON":4.5173,
  128. "RSD":107.6495,
  129. "RUB":83.3695,
  130. "RWF":1058.3464,
  131. "SAR":3.7500,
  132. "SBD":7.8290,
  133. "SCR":14.3972,
  134. "SDG":445.3390,
  135. "SEK":9.4126,
  136. "SGD":1.3582,
  137. "SHP":0.7634,
  138. "SLL":11827.2387,
  139. "SOS":579.5740,
  140. "SRD":20.7475,
  141. "SSP":425.3169,
  142. "STN":22.4125,
  143. "SYP":2511.1358,
  144. "SZL":14.6202,
  145. "THB":33.5163,
  146. "TJS":12.5014,
  147. "TMT":3.5000,
  148. "TND":2.7588,
  149. "TOP":2.2203,
  150. "TRY":14.7205,
  151. "TTD":6.7529,
  152. "TVD":1.3156,
  153. "TWD":28.6146,
  154. "TZS":2322.3918,
  155. "UAH":29.1999,
  156. "UGX":3547.1133,
  157. "UYU":41.2934,
  158. "UZS":11432.7976,
  159. "VES":4.4095,
  160. "VND":22865.8925,
  161. "VUV":110.6141,
  162. "WST":2.5488,
  163. "XAF":600.0677,
  164. "XCD":2.7000,
  165. "XDR":0.7277,
  166. "XOF":600.0677,
  167. "XPF":109.1646,
  168. "YER":250.3338,
  169. "ZAR":14.6364,
  170. "ZMW":17.6268,
  171. "ZWL":142.3748
  172. }
  173. }
ttisahbt

ttisahbt1#

您需要初始化它们。原因之一可能是值为null

  1. data class CurrencyResponse(
  2. @SerializedName("base_code")
  3. val baseCode: String? = null,
  4. @SerializedName("conversion_rates")
  5. val conversionRates: ConversionRates? = null,
  6. @SerializedName("documentation")
  7. val documentation: String? = null,
  8. @SerializedName("result")
  9. val result: String? = null,
  10. @SerializedName("terms_of_use")
  11. val termsOfUse: String? = null,
  12. @SerializedName("time_last_update_unix")
  13. val timeLastUpdateUnix: Int? = null,
  14. @SerializedName("time_last_update_utc")
  15. val timeLastUpdateUtc: String? = null,
  16. @SerializedName("time_next_update_unix")
  17. val timeNextUpdateUnix: Int? = null,
  18. @SerializedName("time_next_update_utc")
  19. val timeNextUpdateUtc: String? = null
  20. )
展开查看全部

相关问题