0
votes

Whenever I am trying to load my gson.fromJsoncommand, it always throws an exception. The error is

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 7 column 2 path

The JSON Result that I am sending is this:

{"status":true,"statuscode":200,"message":"List(s) found!","result":[{"id":"1","name":"Afghan (AFGHANISTAN)","country_code":"AF"},{"id":"2","name":"Albanian (ALBANIA)","country_code":"AL"}],"batch":2,"totalrows":2}

and this is the TestModel:

package com.app.testapp.test.models;

import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.google.gson.annotations.SerializedName;

public class TestModel extends Model {

    @Column(name="id")
    @SerializedName("id")
    String id;

    @Column(name="name")
    @SerializedName("name")
    String name;

    @Column(name="country_code")
    @SerializedName("country_code")
    String country_code;

    public String getTestModelId(){ return id;}

    public void setTestModelId(String TestModelId){
        this.id = TestModelId;
    }

    public String getTestModelName(){ return ("name")

    public void setTestModelName(String TestModelName){
        this.name = TestModelName;
    }

    public String TestModelCode(){ return country_code;}

    public void SetTestModelCode(String TestModelCode){
        this.country_code = TestModelCode;
    }

    @Override
    public String toString(){
        return "Nationality [NationalityId="+id+", NationalityName="+name+" , " +
                "NationalityCode="+country_code+"]";

    }
}

and this is on my httpclient class, all seems to be working well until the error part

   Gson gson = new GsonBuilder().create();

    Type listType = new TypeToken<List<TestModel>>() {
    }.getType();

    try {
        Log.v("MyTag","Try");

        List<TestModel> result = gson.fromJson(new String(responseBody), listType); -- error here

        callback.onGetTestSucess(result);
    } catch (Exception e) {

        callback.onGetTestFailure();

What seems to be the problem with the GSON? Is it because I'm not serializing it properly?

2
You are specifying that the json should be a list (listType), when in fact the json you are parsing is not a list, it is an object. - 1615903
Should I change the list so it would be an object? Or could I change the object to make it into a list? - marchemike
I think you should just add another model like TestModelResponse which has 1 field @SerializedName("result") List<TestModel> models. So when you make a request, you should declare TestModelResponse as a return type for it. - Kistamushken
obviously {"status":true, ... } is not a json array... - Selvin

2 Answers

2
votes

Your JSON is not valid

{
"status": true,
"statuscode": 200,
"message": "List(s) found!",
"result": [
    {
        "id": "1",
        "name": "Afghan (AFGHANISTAN)",
        "country_code": "AF"
    },
    {
        "id": "2",
        "name": "Albanian (ALBANIA)",
        "country_code": "AL"
    }
],
"batch": 2,
"totalrows"2" //Here is your error fix it to "totalrows": 2
} 
0
votes

Your JSON represents an object, like the following one. (Getters, setters, etc. left off for clarity)

class ResponseModel {
    Boolean status;
    Integers statuscode;
    String message;
    List<TestModel> result;
    Integer batch;
    Integer totalrows;
}

You can deserialize with --

ResponseModel response = gson.fromJson(new String(responseBody), ResponseModel);