Json file is not serialized properly - android
I'm trying to get data from API. My app crashes and gives me this message:
kotlinx.serialization.json.internal.JsonDecodingException: Expected
start of the array '[', but had 'EOF' instead
JSON input: .....f2afd0-71e8-5229-9ac9-17d602cd35e4"}],"meta":{"hits":3934}}}
I'm facing this error since a week and I can't find out a solution for it. I need help.
This is the json rsponse:
{"copyright":"Copyright (c) 2022 The New York Times Company. All Rights Reserved.","response":{"docs":[{"abstract":"A career in health journalism has taught me that when it comes to living well, it’s the inner workout that counts the most.","web_url":"https://www.nytimes.com/2022/03/31/well/mind/health-fitness-lessons.html","snippet":"A career in health journalism has taught me that when it comes to living well, it’s the inner workout that counts the most.","lead_paragraph":"When I first started writing about health more than 20 years ago, my columns mostly focused on the physical body: A healthy diet, exercise and screening for disease were regular topics.","source":"The New York Times","multimedia":[{"rank":0,"subtype":"xlarge","caption":null,"credit":null,"type":"image","url":"images/2022/04/05/well/31Well-NL-InnerFitness/31Well-NL-InnerFitness-articleLarge.jpg","height":400,"width":600,"subType":"xlarge","crop_name":"articleLarge","legacy":{"xlarge":"images/2022/04/05/well/31Well-NL-InnerFitness/31Well-NL-InnerFitness-articleLarge.jpg","xlargewidth":600,"xlargeheight":400}},{"rank":0,"subtype":"jumbo","caption":null,"credit":null,"type":"image","url":"images/2022/04/05/well/31Well-NL-InnerFitness/31Well-NL-InnerFitness-jumbo.jpg","height":682,"width":1024,"subType":"jumbo","crop_name":"jumbo","legacy":{}},{"rank":0,"subtype":"superJumbo","caption":null,"credit":null,"type":"image","url":"images/2022/04/05/well/31Well-NL-InnerFitness/31Well-NL-InnerFitness-superJumbo.jpg","height":1166,"width":1750,"subType":"superJumbo","crop_name":"superJumbo","legacy":{}},{"rank":0,"subtype":"thumbnail","caption":null,"credit":null,"type":"image","url":"images/2022/04/05/well/31Well-NL-InnerFitness/31Well-NL-InnerFitness-thumbStandard.jpg","height":75,"width":75,"subType":"thumbnail","crop_name":"thumbStandard","legacy":{"thumbnail":"images/2022/04/05/well/31Well-NL-InnerFitness/31Well-NL-InnerFitness-thumbStandard.jpg","thumbnailwidth":75,"thumbnailheight":75}},{"rank":0,"subtype":"thumbLarge","caption":null,"credit":null,"type":"image","url":"images/2022/04/05/well/31Well-NL-InnerFitness/31Well-NL-InnerFitness-thumbLarge.jpg","height":150,"width":150,"subType":"thumbLarge","crop_name":"thumbLarge","legacy":{}}],"headline":{"main":"For Better Health, Try Fitness From the Inside Out","kicker":null,"content_kicker":null,"print_headline":"","name":null,"seo":null,"sub":null},"keywords":[{"name":"subject","value":"Content Type: Service","rank":1,"major":"N"},{"name":"subject","value":"Anxiety and Stress","rank":2,"major":"N"},{"name":"subject","value":"Meditation","rank":3,"major":"N"},{"name":"subject","value":"Exercise","rank":4,"major":"N"}],"pub_date":"2022-04-01T00:00:08+0000","document_type":"article","news_desk":"Well","section_name":"Well","subsection_name":"Mind","byline":{"original":"By Tara Parker-Pope","person":[{"firstname":"Tara","middlename":null,"lastname":"Parker-Pope","qualifier":null,"title":null,"role":"reported","organization":"","rank":1}],"organization":null},"type_of_material":"News","_id":"nyt://article/0115da20-943c-52dc-8992-817faa170aa1","word_count":1235,"uri":"nyt://article/0115da20-943c-52dc-8992-817faa170aa1"},{"abstract":"He was famous for his portraits of supermodels and his close relationship with Princess Diana. His images are part of the fashion canon.","web_url":"https://www.nytimes.com/2022/03/31/style/patrick-demarchelier-dead.html","snippet":"He was famous for his portraits of supermodels and his close relationship with Princess Diana. His images are part of the fashion canon.","lead_paragraph":"Patrick Demarchelier, a photographer whose work helped define fashion and celebrity in the late 20th and early 21st centuries, died on Thursday. He was 78.","print_section":"A","print_page":"26","source":"The New York Times","multimedia":[{"rank":0,"subtype":"xlarge","caption":null,"credit":null,"type":"image","url":"images/2022/04/03/obituaries/02DEMARCHELIER-print2/00DEMARCHELIER-diana-articleLarge.jpg","height":817,"width":600,"subType":"xlarge","crop_name":"articleLarge","legacy":{"xlarge":"images/2022/04/03/obituaries/02DEMARCHELIER-print2/00DEMARCHELIER-diana-articleLarge.jpg","xlargewidth":600,"xlargeheight":817}},{"rank":0,"subtype":"jumbo","caption":null,"credit":null,"type":"image","url":"images/2022/04/03/obituaries/02DEMARCHELIER-print2/00DEMARCHELIER-diana-jumbo.jpg","height":1024,"width":752,"subType":"jumbo","crop_name":"jumbo","legacy":{}},{"rank":0,"subtype":"superJumbo","caption":null,"credit":null,"type":"image","url":"images/2022/04/03/obituaries/02DEMARCHELIER-print2/00DEMARCHELIER-diana-superJumbo.jpg","height":2048,"width":1504,"subType":"superJumbo","crop_name":"superJumbo","legacy":{}},{"rank":0,"subtype":"thumbnail","caption":null,"credit":null,"type":"image","url":"images/2022/04/03/obituaries/02DEMARCHELIER-print2/00DEMARCHELIER-diana-thumbStandard-v2.jpg","height":75,"width":75,"subType":"thumbnail","crop_name":"thumbStandard","legacy":{"thumbnail":"images/2022/04/03/obituaries/02DEMARCHELIER-print2/00DEMARCHELIER-diana-thumbStandard-.....
These are some of the data classes
#Serializable
data class Article(
#SerialName("copyright")
val copyright: String?,
#SerialName("response")
val response: Response?
)
#Serializable
data class Response(
#SerialName("docs")
val docs: List<Doc>?,
#SerialName("meta")
val meta: Meta?
)
#Serializable
data class Doc(
#SerialName("abstract")
val `abstract`: String?,
#SerialName("byline")
val byline: Byline?,
#SerialName("document_type")
val documentType: String?,
#SerialName("headline")
val headline: Headline?,
#SerialName("_id")
val id: String?,
#SerialName("keywords")
val keywords: List<Keyword>?,
#SerialName("lead_paragraph")
val leadParagraph: String?,
#SerialName("multimedia")
val multimedia: List<Multimedia>?,
#SerialName("news_desk")
val newsDesk: String?,
#SerialName("print_page")
val printPage: String?,
#SerialName("print_section")
val printSection: String?,
#SerialName("pub_date")
val pubDate: String?,
#SerialName("section_name")
val sectionName: String?,
#SerialName("snippet")
val snippet: String?,
#SerialName("source")
val source: String?,
#SerialName("subsection_name")
val subsectionName: String?,
#SerialName("type_of_material")
val typeOfMaterial: String?,
#SerialName("uri")
val uri: String?,
#SerialName("web_url")
val webUrl: String?,
#SerialName("word_count")
val wordCount: Int?
)
This is the provide function for the retrofit instance:
#ExperimentalSerializationApi
#Provides
#Singleton
fun provideRetrofitInstance(okHttpClient: OkHttpClient): Retrofit {
val contentType = "application/json".toMediaType()
return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(json.asConverterFactory(contentType))
.build()
}
you should be using this plugin
this plugin makes your work easy
after installing
and paste all JSON
before using read all documents Link
I recommend using Gson Or moshi to Convert Json Response.
Add dependency in build.Gradle (moshi or gson)
configure retrofit .addConverterFactory(MoshiConverterFactory.create(moshi)) for moshi
Annotate data class with #Json for moshi or #SerializableName for Gson.
Hope it helps !
Related
POSTING a custom object with Retrofit2 issue
I'm currently making my first custom Android app for a project and I can't resolve the following issue. (I made a lot of research but didn't find anything like this) 2020-02-15 11:41:30.075 10337-10337/com.example.quickmatch I/SigninFragmentViewModel: Required value 'surname' missing at $post I'm trying to post this custom object to my backend server online : #JsonClass(generateAdapter = true) data class PlayerObject( val id : Int?, #Json(name = "surname") val surname : String, #Json(name = "first_name") val firstName : String, val pseudo : String, #Json(name = "mdp") val password : String, #Json(name = "mail_address") val mailAddress : String, #Json(name = "phone_number") val phoneNumber : String?, #Json(name = "scored_goals") val scoredGoals : Int, #Json(name = "conceded_goals") val concededGoals : Int, #Json(name = "matches_played") val matchesPlayed : Int, val victories : Int, val avatar : String?, val bio : String? ) I'm using Retrofit2 and Moshi with coroutines : implementation "com.squareup.retrofit2:retrofit:2.5.0" implementation "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2" implementation "com.squareup.retrofit2:converter-moshi:2.5.0" Here is my Retrofit instance : /* Create Moshi object which will parse the responses */ private val moshi = Moshi.Builder() .add(KotlinJsonAdapterFactory()) .build() /* Retrofit builder with converter for response and base url */ private val retrofit = Retrofit.Builder() .addConverterFactory(MoshiConverterFactory.create(moshi)) .addCallAdapterFactory(CoroutineCallAdapterFactory()) .baseUrl(BASE_URL) .build() To send my POST request I made this method (It should send me back the player created if successful) : #POST("...") fun addPlayer(#Body player: PlayerObject) : Deferred<PlayerObject> And I call it passing this Object : var newPlayerObject = PlayerObject(null, name, firstName, pseudo, password, mailAddress, phoneNumber, 0, 0, 0, 0, null, null ) I am getting the non-null values from EditTexts with a basic binding.editText.text.toString() The big problem is that I logged every value (name, surname, etc...) both in my ViewModel before the request and in my UI in the button onClickListener that triggers the ViewModel and everything looks fine and matches what I type in the Edits but when the app posts it seems that every attribute of the object is set to null. Since surname is not nullable I get the previous error. I tried to make it nullable and it is the same issue with the next attributes. Also note that passing an object with every attribute null works.
I've found specifying use-site targets helpful for fields in a Kotlin data class. What happens if you try modifying your annotations to the following format? #field:Json(name = "surname") val surname : String
I finally fixed this issue. The problem was created by the phone_number field which I wanted to be optional. Indeed, when the corresponding EditText was empty when posting, the value wasn't put to null but something "empty" (not just ""), that explain the parsing error. So I just added a condition to set this field at null if the EditText isn't filled.
Parsing tiny JSON from android assets is incredibly slow
I am writing a simple Android app in Kotlin, which will show prayers divided into categories to user. There are 5 JSON files in assets folder, each of them has just around 10 KiB. I use Klaxon for parsing the JSON files into those two data classes: data class Prayer(val prayerName: String, val verseTitle: String, val verseBody: String, val prayerLine: String, val prayerBody: String, val prayerEnding: String) data class PrayerCategory(val title: String, val bgImage: String, val headerImage: String, val prayers : List<Prayer>) Here is the code I use for parsing the prayers: private fun loadPrayerNames(jsonFile: String) { val millis = measureTimeMillis { val input = assets.open("${jsonFile}.json") val prayerCategory = Klaxon().parse<PrayerCategory>(input) if (prayerCategory != null) { for (prayer in prayerCategory.prayers) { val prayerName = prayer.prayerName prayersMap[prayerName] = prayer } } } println("Loading prayer category took $millis ms.") } As you can see, there is just one access to assets. No assets.list(), no bullshit. And as you noticed, I have measured the time.. make your guesses.. Here is the debug output: Loading prayer category took 3427 ms. Yup, that's right. Loading and parsing 10KiB big JSON took 3.5 SECONDS! I repeat. No rocket science involved. Just parsing 10 KiB JSON. 3.5 seconds..... Hmm.. Btw, I am testing it on Nokia 6.1, which is a pretty snappy phone. So.. my questions: What causes this behaviour? Is there any way how to speed this up other than building a database for storing about 50 prayers? I will be very thankful for your help!
Android assets seem to have bad reputation when it comes to performance. However, my testing proved that in this situation, it was Klaxon library who was responsible. After finding major performance problem in Klaxon (see https://github.com/cbeust/klaxon/issues/154), which is still not fixed, I have tried recommended alternative: Moshi (https://github.com/square/moshi). Moshi did improve performance, but parsing my JSON still took about 1 second. After these experiments, I have resorted to the good old fashioned parsing using JSONObject: data class Prayer(val prayerName: String, val verseTitle: String, val verseBody: String, val prayerLine: String, val prayerBody: String, val prayerEnding: String) { companion object { fun parseJson(json: JSONObject) : Prayer = Prayer(json.getString("prayerName"), json.getString("verseTitle"), json.getString("verseBody"), json.getString("prayerLine"), json.getString("prayerBody"), json.getString("prayerEnding")) } } data class PrayerCategory(val title: String, val bgImage: String, val headerImage: String, val prayers : List<Prayer>) { companion object { fun parseJson(json: JSONObject): PrayerCategory { val prayers = ArrayList<Prayer>() val prayersArray = json.getJSONArray("prayers") for(i in 0 until prayersArray.length()) { prayers.add(Prayer.parseJson(prayersArray.getJSONObject(i))) } return PrayerCategory(json.getString("title"), json.getString("bgImage"), json.getString("headerImage"), prayers) } } } This has reduced parsing time from 3427 ms to 13ms. Case closed. ;-)
Fold with Kotlin in a property of a data class model in an Android app
I'm not sure the title is clarifying enough, so I'll explain here: I have four models: This model is the one I use for parsing the JSON from a file #JsonClass(generateAdapter = true) data class Account( val account: String, val balance: String, val transactions: List<Transaction> ) This Account has a list of Transactions: #JsonClass(generateAdapter = true) data class Transaction( val id: String, val amount: String, val description: String, val otherAccount: String, val date: String ) For the UI layer, I have some equivalent models: data class AccountEntity( val account: String, val balance: String, val transactions: List<TransactionEntity> ) and data class TransactionEntity( var amount: BigDecimal, var balanceBefore: BigDecimal, var balanceAfter: BigDecimal, val description: String, val otherAccount: String, val date: Date ) The tricky thing I want to do is, that an Account has a balance and I need to show in every TransactionData the balanceBefore and the balanceAfter which are respectively, the balance of the account BEFORE this transaction was made, and the balance of the account AFTER this transaction was made. For me this is a bit tricky not only to understand but also to implement. I thought of maybe implementing some sort of fold operation in every transaction, but I'm not sure how to apply it only in the balanceBefore and the balanceAfter properties. Any other idea? Thanks a lot in advance!
Well, it's some sort of reduce/fold function that also allows you to map your Transaction objects. What this function do is to track the current balance in an accumulator and transform each Transaction object: inline fun <R> List<Transaction>.map( initial: Int, transform: (transaction: Transaction, beforeBalance: Int, afterBalance: Int) -> R ): List<R> { var accumulator = initial val destination = ArrayList<R>() for (transaction in this) { val after = accumulator - transaction.amount destination.add(transform(transaction, accumulator, after)) accumulator = after } return destination } And you can use it like: account.transactions.map(account.balance, {t, b, a-> TransactionEntity(...)}) To be more generic and functional, you can move this line accumulator - transaction.amount to the function parameter as a fold input, and call the function foldMap or whatever which reduce your current transaction and also map it.
Retrofit - Android - Handling multiple type keys and values with gson
So I've been having some trouble with an api that I want to consume from my application when dealing with multiple types from the keys and values of the response json. Let me show you the json response: { "error":[ ], "result":{ "field1":[ [ 1544258160, "57.15", "57.15", "57.15", "57.15", "0.00", "0.00000000", 0 ], [ 1544258220, "56.89", "56.89", "56.89", "56.89", "56.89", "2.94406281", 1 ] ], "field2":1544301240 } } and here is the representation of the pojo class: data class Response(val error: List<String>, val result: LinkedTreeMap<String, List<List<Result>>>) data class Result( val time: Double, val open: String, val high: String, val low: String, val close: String, val vwap: String, val volume: String, val count: Double ) I know that the current structure fails to represent the json format. but I have run out of ideas. btw the stack error is saying this: Expected BEGIN_OBJECT but was NUMBER edit: adding a bit more context I'm using Gsonconverter for the retrofit builder. val retrofit = Retrofit.Builder().baseUrl(API_URL).client(client) .addConverterFactory(GsonConverterFactory.create()).build()
You could build a Gson JsonDeserializer for the filed1, filed2 types. It is more manually written code, but this way you can check the type of the filed and call the right deserializer.
yeah,what make the error is your response field(result),it's different type.however.you just use the first type Response(val error: List<String>, val result:LinkedTreeMap<String, List<List<Result>>> receive all the json. in my view,you can use below ways to solve it. First,redefine you receive model,maybe you can use the Gson JsonObject and when deal with value remember check the type. Second,discuss with your background server engineer,agree on the every type response.may be the "field2" can be "field2:[[]]" Next,change your model.may be you define below it #Serialized("field1") LinkedTreeMap<String, List<List<Result>>> field; #Serialized("field2") String fields; hope to help you.
Your problem is here: "field2":1544301240 You defined like below: val result: LinkedTreeMap<String, List<List<Result>>> but instead of that you get number!!! EDIT Just try this: data class Response(val error: List<String>, val result: YourModel) data class YourModel(val field1: LinkedTreeMap<String, List<List<Result>>>, val field2: Double) data class Result( val time: Double, val open: String, val high: String, val low: String, val close: String, val vwap: String, val volume: String, val count: Double )
it has been a while but still i already came to a conclusion of what i need. data class OHLCResponse( private val error: List<String>, val result: LinkedTreeMap<String, Any>) turns out that on the LinkedTreeMap class i just needed to pass the second class type parameter as Any and it can be down casted to anything that you need. also i leave here a nice tool that can help you map your json responses into kotlin plain objects/DTOs: https://app.quicktype.io/ thanks all for your responses.
I solved the problem with such a solution in adapter class . override fun getItemCount(): Int = leagueTableList[0].size override fun onBindViewHolder(holder: LeagueTableViewHolder, position: Int) { val gs = Gson() val js = gs.toJson(leagueTableList[0][position]) val standing = gs.fromJson(js, Standing::class.java) holder.view.table = standing holder.bind(standing,onItemClick) }
Kotlin - Retrofit request using RxJava gives null response
I'm trying to get news from Guardian API. I'm getting null response, everything is below. I'm using Kotlin, Retrofit and RxJava. I know that there are some miscalled variables/objects but I will change them when I will get rid of that problem. Retrofit interface #get:GET("search?api-key=test") val news:Observable<News> Retrofit client val instance : Retrofit get() { if (myInstance == null) { myInstance = Retrofit.Builder() .baseUrl("https://content.guardianapis.com/") .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build() } return myInstance!! } And function where I'm loading data private fun loadUrlData() { compositeDisposable.add(jsonApi.news .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe{news -> displayData(news)}) } JSON example { response:{ status:"ok", userTier:"developer", total:2063064, startIndex:1, pageSize:10, currentPage:1, pages:206307, orderBy:"newest", results:[ { id:"politics/2018/sep/24/keir-starmer-labour-does-not-rule-out-remaining-in-eu", type:"article", sectionId:"politics", sectionName:"Politics", webPublicationDate:"2018-09-24T18:57:48Z", webTitle:"Keir Starmer: Labour does not rule out remaining in EU as option", webUrl:"https://www.theguardian.com/politics/2018/sep/24/keir-starmer-labour-does-not-rule-out-remaining-in-eu", apiUrl:"https://content.guardianapis.com/politics/2018/sep/24/keir-starmer-labour-does-not-rule-out-remaining-in-eu", isHosted:false, pillarId:"pillar/news", pillarName:"News" } ] } } Model class data class News( val status: String, val userTier: String, val total: Int, val startIndex: Int, val pageSize: Int, val currentPage: Int, val pages: Int, val orderBy: String, val results: List<Result>) I suppose that the problem is with the last function or with the interface but I can't find the solution.
The issues lies within your data model class. Your JSON has an outer node (response) and if you're trying to return a News you won't get it, because Retrofit can't map the JSON to the News class. Add an outer class called Response that holds a field called response that is of type News, that should fix it. Like so: class Response(val response: News) Note: I haven't added data in front of the class as you don't necessarily need it. The data keyword just adds some extra things for you automatically, like toString(), equals() and hashCode(), but unless you're actually using them for anything I wouldn't recommend adding the data keyword as it's pretty useless then.