• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Reason for exception

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.Gson.fromJson(Gson.java:944)
at com.google.gson.Gson.fromJson(Gson.java:897)
at com.google.gson.Gson.fromJson(Gson.java:846)
at com.onedevice.admin.MainActivity$15.onResponse(MainActivity.java:886)
at com.onedevice.admin.RequestNetworkController$3$2.run(RequestNetworkController.java:179)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6748)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:384)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:183)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
at com.google.gson.Gson.fromJson(Gson.java:932)
... 11 more
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid we can't help from the information you have given us. Please supply lots more details. I have moved you to what I think is a more appropriate location. The stack trace mentioned Android, so I moved you to that forum; I don't know any Android myself.
 
Marshal
Posts: 28296
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is that the JSON you're trying to process is badly formed. So that JSON should be included in the details you post next.
 
Marshal
Posts: 4600
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:My guess is that the JSON you're trying to process is badly formed.


I agree - the message Expected BEGIN_OBJECT but was STRING is telling you that something other than an opening curly-bracket was found when trying to parse an object.
 
Ron McLeod
Marshal
Posts: 4600
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are getting the content from a network endpoint:
  - check to the return status to ensure that the request was processed properly (for example - with HTTP: a 200 response)
  - ensure that the content type provided that the endpoint was as-expected (for example - with HTTP: Content-Type: application/json)
  - ensure that the content length is greater than zero

It would also be helpful to log the actual content so that you can visually inspect what was returned (or if more complex, copy/paste it in to an on-line JSON parser such as JSON Formatter.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Things to check quickly:
1. If you are using Gson, Kotlinx-serialization... basically, anything that is not the bad old JSONObject, make sure that it is configured to skip missing/redundant fields.
2. Did you try to just copy-paste your JSON to some JSON-lint site? https://jsonlint.com/ Note that when copy-pasting the JSON, Android Studio makes a mess with the quotes.
3. Alternatively, did you try to parse it with JSONObject(json) just to make sure it's legal.
That could be a hint.

Where do you go to from here?
1. If the JSON is legal, just make sure your parser can eat missing/redundant fields.
2. If not - the Achiles Heel is always the quotes. People forget to add them, or add them wrong.
3. If you are using KTOR or Retrofit - make sure that the headers are application/json. I've seen cases where it's fed wrong.
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic