0

I was following this tutorial for gson-android

http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

My json is

{"IsAuthenticated":true,"BusinessId":57,"Success":true,"ErrorId":0}

I have created my model object:

LoginResponse.java

import com.google.gson.annotations.SerializedName;
import com.xxxx.xxxx.R.string;

public class LoginResponse {

    @SerializedName("Sucess")
    private Boolean sucess;

    @SerializedName("IsAuthenticated")
    private Boolean isAuthenticated;

    @SerializedName("BusinessId")
    private String businessId;

    @SerializedName("ErrorId")
    private string errorId;

    public Boolean getSucess() {
        return sucess;
    }

    public void setSucess(Boolean sucess) {
        this.sucess = sucess;
    }

    public Boolean getIsAuthenticated() {
        return isAuthenticated;
    }

    public void setIsAuthenticated(Boolean isAuthenticated) {
        this.isAuthenticated = isAuthenticated;
    }

    public String getBusinessId() {
        return businessId;
    }

    public void setBusinessId(String businessId) {
        this.businessId = businessId;
    }

    public string getErrorId() {
        return errorId;
    }

    public void setErrorId(string errorId) {
        this.errorId = errorId;
    }

}

My app is getting force close after running this code.

LOGCAT

    07-04 12:22:40.682: E/AndroidRuntime(2694): FATAL EXCEPTION: AsyncTask #3
07-04 12:22:40.682: E/AndroidRuntime(2694): java.lang.RuntimeException: An error occured while executing doInBackground()
07-04 12:22:40.682: E/AndroidRuntime(2694):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at java.lang.Thread.run(Thread.java:856)
07-04 12:22:40.682: E/AndroidRuntime(2694): Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER at line 1 column 67
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.google.gson.Gson.fromJson(Gson.java:803)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.google.gson.Gson.fromJson(Gson.java:741)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.example.xxxx.loginActivity.ValidateUser(LoginActivity.java:300)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.example.xxxx.loginActivity.access$0(LoginActivity.java:276)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.example.xxxx.loginActivity$UserLoginTask.doInBackground(LoginActivity.java:222)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.example.xxxx.loginActivity$UserLoginTask.doInBackground(LoginActivity.java:1)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-04 12:22:40.682: E/AndroidRuntime(2694):     ... 4 more
07-04 12:22:40.682: E/AndroidRuntime(2694): Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER at line 1 column 67
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
07-04 12:22:40.682: E/AndroidRuntime(2694):     ... 14 more

.. and this is how i did that

    HttpResponse response = client.execute(httpGet);

    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode == 200) {
        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(content));


        Gson gson = new Gson();
        LoginResponse myResponse = gson.fromJson(reader, LoginResponse.class);
        Log.d(TAG, myResponse.getBusinessId().toString());



    } else {
        Log.e(TAG, "Failed to download file");
    }

Am I doing something wrong? I just followed the things in the tut. Could anyone help me.

UPDATE:

after correcting string to String and other things, I was able to fix the app crash. Still I am unable to get the object.

the updated log here

07-08 05:02:53.625: W/System.err(869): org.json.JSONException: End of input at character 0 of 
07-08 05:02:53.656: W/System.err(869):  at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
07-08 05:02:53.656: W/System.err(869):  at org.json.JSONTokener.nextValue(JSONTokener.java:97)
07-08 05:02:53.656: W/System.err(869):  at org.json.JSONObject.<init>(JSONObject.java:154)
07-08 05:02:53.656: W/System.err(869):  at org.json.JSONObject.<init>(JSONObject.java:171)
07-08 05:02:53.656: W/System.err(869):  at com.example.xxxx.LoginActivity$UserLoginTask.onPostExecute(LoginActivity.java:238)
07-08 05:02:53.656: W/System.err(869):  at com.example.xxxx.LoginActivity$UserLoginTask.onPostExecute(LoginActivity.java:1)
07-08 05:02:53.656: W/System.err(869):  at android.os.AsyncTask.finish(AsyncTask.java:631)
07-08 05:02:53.656: W/System.err(869):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-08 05:02:53.656: W/System.err(869):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
07-08 05:02:53.675: W/System.err(869):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 05:02:53.675: W/System.err(869):  at android.os.Looper.loop(Looper.java:137)
07-08 05:02:53.675: W/System.err(869):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-08 05:02:53.675: W/System.err(869):  at java.lang.reflect.Method.invokeNative(Native Method)
07-08 05:02:53.675: W/System.err(869):  at java.lang.reflect.Method.invoke(Method.java:511)
07-08 05:02:53.675: W/System.err(869):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-08 05:02:53.675: W/System.err(869):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-08 05:02:53.675: W/System.err(869):  at dalvik.system.NativeStart.main(Native Method)

I dead googled this , but didn't get a solution

1
  • try this, hold the json values in string type.. Commented Jul 4, 2013 at 12:45

4 Answers 4

1

your

   @SerializedName("BusinessId")
    private String businessId;

    @SerializedName("ErrorId")
    private string errorId;

should be Integers

  @SerializedName("BusinessId")
    private int businessId;

    @SerializedName("ErrorId")
    private int errorId;
Sign up to request clarification or add additional context in comments.

1 Comment

Gson doesn't care. It will convert numbers to String automatically. That's not what that error means.
0

The error you are receiving from Gson is telling you that it's expecting to find a { at the 67th column (character) in your JSON. This means errorId is an object type in your Java class, not a number (e.g. int, long) or a String (see last paragraph below regarding automatic String conversions).

Looking at your code, you have ...

@SerializedName("ErrorId")
private string errorId;

Apparently you have created a string class (lowercase s), have used it, and Gson is trying to deserialize to it. This is why you should never do that. First, classes is Java should start with an uppercase letter. Second you should never create classes with the same names as system ones. (Option B would be that the Java class you show isn't what is actually running and there's some other object type there).

If you fix that by changing it to a String the error will go away. Even though errorId (and businessId) is now a String in your Java, Gson will actually automatically convert number types in JSON to a String type in a Java class. You also need to fix the spelling error in sucess or it will end up being null because that doesn't exist in the JSON and Gson silently ignores any missing elements.

Comments

0

There is a typo in your LoginResponse.java:

@SerializedName("Sucess")

should be

@SerializedName("Success")

Maybe there're more problems though, could you check that the JSON you're trying to parse is indeed what you expect it to be?

4 Comments

The variable sucess should be renamed to success too.
The variable name shouldn't matter if it's annotated, right? Of course, its always good to keep your spelling correct :)
Absolutely- and of course if you misname the json key behaviour should be that it just doesn't get set!
now its org.json.JSONException: End of input at character 0 of ( please find the log in the updated question )
0

Is private string errorId; really lower case there? You might want String, or int (both will work).

Reflectiony stuff in gson reads your field type and thinks it must be an object that a nested json object can be mapped to. Could explain Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER at line 1 column 67: your reflectiony mapper is expecting an object (but not java.lang.String) type for ErrorId because you've given it something it thinks is another kind of object- mysterious string. String, however, is a class it knows how to deal with.

Comments

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.