Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8620209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:31:39+00:00 2026-06-12T06:31:39+00:00

i am using mongodb as cloud server for my android app.Mongodb java library doesn’t

  • 0

i am using mongodb as cloud server for my android app.Mongodb java library doesn’t work in Android so i am using rest api .i wrote the following code to get the json response and cast them into MyClass but its giving IllegalStateException with

    java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_Object at line 1 column 35

Code :

              try {  HttpGet mRequest = new HttpGet("https://api.mongolab.com/api/1/databases/mydb/collections/mycol?apiKey=myKey");    

                     DefaultHttpClient client = new DefaultHttpClient();
   try {
              HttpResponse response = client.execute(mRequest);

              InputStream source = response.getEntity().getContent();

              Reader reader = new InputStreamReader(source);

              Gson gson = new Gson();

          Type typeOfCollectionOfMyObject = new TypeToken<Collection<MYObject>>(){}.getType();

          quizDBObjectList = gson.fromJson(reader, typeOfCollectionOfMyObject);

          } catch (IOException e) {
          mRequest.abort();
          }

this is not the whole code but i am getting the exception in this block.Any Help ?

Definition of MYObject is

           public class MYObject { 

                   private long index ;
                   private String question;
                   private String answer;
                   private String optionA;
                   private String optionB;
                   private String optionC;
                   private String optionD;
                   private String createdAt;
                   private String active;
                //getters and setters of data members
      }

and the response i got was :

                           [ { "_id" : { "$oid" : "505ab996aded66f4c1ccc7f2"} , "index" : 0 , "question" : "some text ?" , "optiona" : "optionA" , "optionb" : "optionB" , "optionc" : "OptionC" , "optiond" : "optionD" , "answer" : "answer" , "created_at" : { "$date" : "2012-09-20T06:37:04.306Z"} , "Active" : "1"} , { "_id" : { "$oid" : "505ab997aded66f4c1ccc7f3"} , "index" : 1 , ..../objects like that]

clearly i was not aware of the fact that the _id would be responded by the server.
Can you give me the correct class definition to cast this json object into my customized class. thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:31:40+00:00Added an answer on June 12, 2026 at 6:31 am

    java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_Object at line 1 column 35

    This says it all, there should be somewhere [ instead of {. Maybe you get a single object instead of an array?

    The above might help or not. In order to get a better answer the following is needed:

    • definition of MYObject
    • the exact parsed String

    You can get the parsed String e.g. by reading from reader into a StringBuider or simpler by using Guava’s CharStreams.toString(reader).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using MongoDB via official Java API. I can store and retrive Long
I am using mongodb in java for one of my projects. User is going
I am using mongoDB for a new project at work and I got into
Im using mongodb + java. I tried to insert a new document into an
I'm considering using MongoDb for a backing store for a WPF app that I'm
I am using Mongodb Java driver for my Java client application. it needs to
Using MongoDB I'm trying to copy a database from one server to another. My
I am using MongoDB in our project and I'm currently learning how things work.
I'm using MongoDB and MongoID in a rails app, how can some models be
I am using mongodb-v1.8.1. Unfortunately my server gets hanged and i restarted my server.Once

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.