4

I want to read the result of a GET method of REST services, using a Client Application that I build using HttpUrlConnection. That method returns information about an User. After read it, I want to create an object of the User type, with all the information of that User filled. I think I have to convert it into JSON first, right? I'm using GSON.

What I have is:

if(urlConnection.getResponseCode()==200)
{
   String response ="";
   Scanner inStream = new Scanner(urlConnection.getInputStream());

   while(inStream.hasNextLine())
      response+=(inStream.nextLine());
   System.out.println(response);

   //JSON
   Gson gson = new Gson();
   String json = gson.toJson(response);
   System.out.println(json);

   // User Object
   User object = new User();
   object = gson.fromJson(json, User.class);
   System.out.println(object);

}

When I do the first print, I receive:

{"userID":"user2","isMale":false,"isObject":false,"telephone":"+911111111","email":"[email protected]","birthdate":"2012-08-01","firstName":"Maria","lastName":"Silva","isocountrycode":"PT"}

When I do the second print, I receive:

"{\"userID\":\"user2\",\"isMale\":false,\"isObject\":false,\"telephone\":\"+911111111\",\"email\":\"[email protected]\",\"birthdate\":\"2012-08-01\",\"firstName\":\"Maria\",\"lastName\":\"Silva\",\"isocountrycode\":\"PT\"}"

But when I try to print the User object I receive this error:

    com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 226
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
    at com.google.gson.Gson.fromJson(Gson.java:795)
    at com.google.gson.Gson.fromJson(Gson.java:761)
    at com.google.gson.Gson.fromJson(Gson.java:710)
    at com.google.gson.Gson.fromJson(Gson.java:682)
    at httpURLconnection.UserGetUserInfo.main(UserGetUserInfo.java:70)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 226
    at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
    ... 5 more

Can you tell me what am I doing wrong? All the User object fields were filled using that User class.

User class:

    public class User {
    String  userID        = null;
    boolean isMale        = false;
    boolean isObject      = false;
    String  Telephone     = null;
    String  Email         = null;
    Date    Birthdate     = null;
    String  FirstName     = null;
    String  LastName      = null;
    String  ISOcountrycode   = null;

    (...) 
    }
1
  • 1
    The same Problem i am getting right now ? How you have resolved your issue? Commented Dec 30, 2013 at 7:04

1 Answer 1

1

In your USER class you have to declare all datatypes, not more and not less, which the JSONObject contains. F.e.: class User { String UserID = ""; Boolean isMale;}

and so on.

Otherwise the GSON.toJson(); will not work and throws the exception.

Sign up to request clarification or add additional context in comments.

7 Comments

Right.. I have this in my User class: public class UserT { String userID = null; String FirstName = null; String LastName = null; String ISOcountrycode = null; String Telephone = null; boolean isMale = false; String Email = null; Date Birthdate = null; boolean isObject = false;
Is it because the order isn't the same? But I now tried with the same order and the error is the same.
Maybe its the class name. UserT is your class in the comments but you use User.class in your question. Maybe its an order problem also but i never recognized this.
Didnt saw it on the first try... You dont have to call String json = gson.toJson(response); because your String is already in JSON-Format. So just call object = gson.fromJson(response, User.class);. Should work fine.
Your right. But I only get my first parameter (userId). All the others are 'null'...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.