Jack
Jack

Reputation: 1855

Retrofit JSON parsing issues in retrieving value from list ? How to solve?

I have A NESTED JSON to parse, for instance ;

 [
{
    "ID": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "68613",
        "IsBinaryUnique": true
    },
    "NATIVE_DATE": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "2015/02/22",
        "IsBinaryUnique": true
    },
    "ANNOUNCEMENT_DATE": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "February 22,  2015",
        "IsBinaryUnique": true
    },
    "EXPIRY": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "",
        "IsBinaryUnique": true
    },
    "ANNOUNCEMENT_DESCRIPTION": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "",
        "IsBinaryUnique": true
    },
    "ANNOUNCEMENT_TITLE": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "Emirates Motor Company unveils all-new Mercedes-Benz 2015 C-Class",
        "IsBinaryUnique": true
    },
    "ANNOUNCEMENT_IMAGE": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "HTTP://94.56.199.34/EMC/EMCPortal/DesktopModules/Farabi.ICatalog/CatalogImages/488/C-2048_sd-eng.jpg",
        "IsBinaryUnique": true
    },
    "ANNOUNCEMENT_IMAGE_THUMBNAIL": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "http://94.56.199.34/EMC/IPDP/thumbnail.ashx?File=/DesktopModules/Farabi.ICatalog/CatalogImages/488/C-2048_sd-eng.jpg",
        "IsBinaryUnique": true
    },
    "ANNOUNCEMENT_HTML": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "<!--NEWS-->\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head>\r\n    <meta name=\"viewport\" content=\"width=device-width\" />\r\n    <title>News & Promotions</title>\r\n    <style type=\"text/css\">\r\n        H1\r\n        {\r\n            text-align: left;\r\n            color:Black;\r\n            font-size:22px;\r\n            font-weight: bold;\r\n            font-family:Arial;\r\n        }\r\n        Body\r\n        {\r\n            background-color: transparent\";\r\n            font-family:Arial;\r\n        }\r\n        .Header\r\n        {\r\n            -webkit-border-top-left-radius: 10px;\r\n            -webkit-border-bottom-left-radius: 10px;\r\n            -moz-border-radius-topleft: 10px;\r\n            -moz-border-radius-bottomleft: 10px;\r\n            border-top-left-radius: 10px;\r\n            border-bottom-left-radius: 10px;\r\n            background-color: #0e100f\";\r\n            margin-top: 10px;\r\n            font-weight: bold;\r\n            border: 5 none transperant;\r\n            font-family:Arial;\r\n        }\r\n        .SubTitle\r\n        {\r\n            color: black;\r\n            font-weight: bold;\r\n            font-size:16px;\r\n            font-family:Arial;\r\n        }\r\n        \r\n        .ContentText\r\n        {\r\n            color: black;\r\n            font-size:16px;\r\n            font-family:Arial;\r\n        }\r\n        \r\n        .Content\r\n        {\r\n            margin-left: 5px;\r\n        }\r\n    </style>\r\n</head>\r\n<body leftmargin=\"0\" topmargin=\"0\">\r\n    <table border=\"0\" cellpadding=\"5\" cellspacing=\"5\" >\r\n        <tr>\r\n            <td>\r\n                <h1>\r\n                    Emirates Motor Company unveils all-new Mercedes-Benz 2015 C-Class\r\n                </h1>\r\n             </td>\r\n        </tr>\r\n        <tr class=\"SubTitle\">\r\n             <td align=\"right\">\r\n                  February 22,  2015\r\n              </td>\r\n        </tr>\r\n        <tr class=\"SubTitle\">\r\n             <td align=\"right\">\r\n                 Valid Till &nbsp $Expired_Date\r\n              </td>\r\n        </tr>\r\n        <tr>\r\n            <td>\r\n                                <img alt=\"\" src=\"http://94.56.199.34/EMC/EMCPortal/DesktopModules/Farabi.ICatalog/CatalogImages/488/C-2048_sd-eng.jpg\" style=\"width: 100%; float: right\" hspace=\"0\" />\r\n                </td>\r\n        </tr>\r\n        <tr>\r\n            <td>     \r\n                <div class=\"Content\" valign=\"top\">\r\n                    <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">\r\n                        <tr>\r\n                            <td class=\"ContentText\" align=\"left\">\r\n                                <p><span>Emirates Motor Company (EMC), General Distributors of Mercedes-Benz in the Emirate of Abu Dhabi and the flagship company of Al Fahim Group, launched the all-new Mercedes-Benz 2015 C-Class in a gala iftar and a superb unveiling of the fabulous new innovative car on Saturday, 5 July at their close-to-heart Airport Road Showroom in Abu Dhabi.</span></p>\r\n<p><span>In the presence of newly appointed CEO Automotive of Al Fahim Group, Mr. Frank Bernthaler, the celebrative evening continued with a press conference where EMC hosts Mr. Kamal Rafih, General Manager, Mr. Jordan Duckett, General Sales Manager, and Mr. Rabih Eid, Operations Manager, were head-table spokespersons.</span></p>\r\n<p><span>\"We are delighted to be revealing the admirable all-new Mercedes-Benz 2015 C-Class during a blessed, festive month and announce the great achievements we've accomplished so far this year,\" praised Mr. Rafih in his speech to the media. \"As we conclude the first half of the year, we also celebrate a well-earned increase in our sales performance of 42% compared to the same period last year,\" he added.</span></p>\r\n<p><span>Among the most popular models in 2014 which witnessed an exponential rise were the legendary Mercedes-Benz S-Class by an impressive 347%, the sporty A-Class by 156%, the luxurious E-Class by 46%, and the entire sports utility vehicle (SUV) range by 22%, which includes the G-Class, GL-Class, GLK Class and the M-Class.</span></p>\r\n<p><span>During the memorable occasion, media representatives and guests were also invited to explore the latest innovation by Mercedes-Benz and register for the driving experience. The all-new C-Class sets new standards in the premium medium-size category in terms of striking appearance, cutting-edge technology, spacious comfort and refined sportiness. A host of new assistance systems offers safety of the highest standard, while a new optional air-sprung suspension provides for exemplary ride and driving comfort as well as nimble and agile handling.</span></p>\r\n<p><span>Thanks to intelligent and innovative lightweight construction, the aluminum hybrid body is around 70 kilograms lighter than a conventional steel body. Compared with the successful predecessor, the aluminum content has increased from less than 10% to almost 50%. As a result, the vehicle's overall weight is reduced by up to 100 kilograms. Furthermore, with an 80-millimeter increase in the wheelbase compared with the previous model, the vehicle is 95 millimeters longer and 40 millimeters wider. The result is a more spacious interior.</span></p>\r\n<p><strong><span>High-class interior with fine details</span></strong><span><br />\r\nThe innovative touchpad developed by Mercedes-Benz in the hand-rest marks a further evolutionary step. As on a smartphone, this provides for simple and intuitive operation of all the head-unit functions using finger gestures. The touchpad also permits letters, numbers and special characters to be entered in handwriting - in any language supported by Audio 20. One of the main highlights of the innovative touchpad is that Arabic letters, numbers and even special characters are supported, and can be entered into the handwriting recognition function.</span></p>\r\n<p><strong><span>Mercedes-Benz Intelligent Drive: the intelligent car</span></strong><span><br />\r\nThe new C-Class incorporates almost all of the new assistance systems with a host of enhanced functions which celebrated their world premiere in the S-Class and E-Class only some months ago. The assistance systems enhance both comfort and safety. Mercedes-Benz calls this Intelligent Drive.</span></p>\r\n<p><span>The new C-Class offers numerous innovative safety and assistance systems. It is equipped as standard with ATTENTION ASSIST, which can warn the driver of inattentiveness and fatigue. In addition to Adaptive Brake Assist, which offers protection from collisions from speeds as low as 7 km/h, standard-fit COLLISION PREVENTION ASSIST PLUS also features an additional function: when the risk of collision persists and the driver fails to respond, the system is able to carry out autonomous braking at speeds of up to 200 km/h, thereby reducing the severity of collisions with slower or stopping vehicles. The system also brakes in response to stationary vehicles at a speed of up to 50 km/h, and is able to prevent rear-end collisions at up to 40 km/h.</span></p>\r\n<p><strong><span>Other safety features include:</span></strong> <span><br />\r\n-DISTRONIC PLUS with Steering Assist and integrated Stop&amp;Go Pilot\r\n<br />\r\n-BAS PLUS Brake Assist and PRE-SAFE&reg; Brake\r\n<br />\r\n-Enhanced Active Lane Keeping Assist\r\n<br />\r\n-Active Parking Assist\r\n<br />\r\n-Traffic Sign Assist\r\n<br />\r\n-Adaptive Highbeam Assist Plus</span></p>\r\n                            </td>\r\n                        </tr>\r\n                    </table>\r\n                </div>\r\n            </td>\r\n        </tr>\r\n    </table>\r\n</body>\r\n</html>\r\n",
        "IsBinaryUnique": true
    }
},
{
    "ID": {
        "Tag": "string",
        "TypeCode": 5,
        "Value": "66055",
        "IsBinaryUnique": true
    }, etc....}]

I need to get the announcement title values, in order to do this I have created a model class for Announcement Title.

public class AnnouncementTitle {


@SerializedName("Tag")
private String tag;

@SerializedName("TypeCode")
private String typecode;

@SerializedName("Value")
private String value;

@SerializedName("IsBinaryUnique")
private boolean isbinary;

public AnnouncementTitle(String tag, String typecode, String value, boolean isbinary) {
    this.tag = tag;
    this.typecode = typecode;
    this.value = value;
    this.isbinary = isbinary;
}

public String getTag() {
    return tag;
}

public void setTag(String tag) {
    this.tag = tag;
}

public String getTypecode() {
    return typecode;
}

public void setTypecode(String typecode) {
    this.typecode = typecode;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public boolean isIsbinary() {
    return isbinary;
}

public void setIsbinary(boolean isbinary) {
    this.isbinary = isbinary;
}

}

and my API class is like,

public interface Api {

String BASE_URL ="base url";
@GET("url to get values from")
Call<ArrayList<AnnouncementTitle>> getData();}

I have implemented the retrofit call in MainActivity.java

 public void getData() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Api.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    Api api = retrofit.create(Api.class);

    Call<ArrayList<AnnouncementTitle>> call = api.getData();

    call.enqueue(new Callback<ArrayList<AnnouncementTitle>>() {


        @Override
        public void onResponse(Call<ArrayList<AnnouncementTitle>> call, Response<ArrayList<AnnouncementTitle>> response) {



            ArrayList<AnnouncementTitle> response_array = response.body();

            Log.e("Response "," "+ response_array);
            Log.e("Response "," "+ response_array.size());

            Log.e("Response ","Value "+ response_array.get(0).getValue());


            for (int i = 0;i<response_array.size();i++){
                AnnouncementTitle announcementTitle= response_array.get(i);
                String a = announcementTitle.getTag();
                String b = announcementTitle.getValue();
                String c = announcementTitle.getTypecode();

                //Log.e("Response ","Value "+ b);

                // Log.e("Response ","Value "+ c);
            }




        }

        @Override
        public void onFailure(Call<ArrayList<AnnouncementTitle>> call, Throwable t) {

            Log.e("Failed "," ");
        }
    });
}}

My problem is I get the response as array list correctly, but the values inside ArrayList are null for instance from the above code the string values a,b,c, all of them returning value as null.Can anyone help to find and resolve this problem?.

Upvotes: 0

Views: 189

Answers (3)

Abdul Hanan
Abdul Hanan

Reputation: 93

copy your all json response and change it to pojo class from the link https://www.jsonutils.com/ and make the get set method in class

your interface will become like that

public interface Api {

@GET("url to get values from")
Call<PojoClassName> getData();
}

retrofit class look like that

Api api = retrofit.create(Api.class);

Call<PojoClassName> call = api.getData();

call.enqueue(new Callback<PojoClassName>() {


    @Override
    public void onResponse(Call<PojoClassName> call, Response<PojoClassName> response) {
        Log.e("Response "," "+response.body().toString);
        Log.e("Response "," "+response.body().getID().getTag());
        Log.e("Response "," "+response.body().getEXPIRY().getTag());
    }

    @Override
    public void onFailure(Call<PojoClassName> call, Throwable t) {

        Log.e("Failed ",t.getMessage());
    }
});

Upvotes: 0

Jay Thummar
Jay Thummar

Reputation: 2299

Get JSON object from list like this

JSONObject obj = responseArray.get(i);
JSONObject ID = obj.getJSonObject("ID");

Variables in your model class should be like this (I think you forgot to set TypeCode to declare Integer)

@SerializedName("Tag")
private String tag;

@SerializedName("TypeCode")
private int typecode;

@SerializedName("Value")
private String value;

@SerializedName("IsBinaryUnique")
private boolean isbinary;

Pass the data in to model class like this in for loop

AnnouncementTitle  model = new AnnouncementTitle(
   ID.getString("TAG"),
   ID.getInt("TypeCode"),
   ID.getString("Value"),
   ID.getBoolean("IsBinaryUnique")
);

Upvotes: 0

isabsent
isabsent

Reputation: 3773

First: JSON you are showing is invalid. Apart from invalid braces placement if you declare

@SerializedName("TypeCode")
private String typecode;

you will have

"TypeCode": "5"

instead of

"TypeCode": 5

in JSON. The correct JSON representing your an ArrayList<AnnouncementTitle> is:

[
{"ID": {
    "Tag": "string",
    "TypeCode": "5",
    "Value": "68613",
    "IsBinaryUnique": true}
},

{"NATIVE_DATE": {
    "Tag": "string",
    "TypeCode": "5",
    "Value": "2015/02/22",
    "IsBinaryUnique": true}
},

{"ANNOUNCEMENT_TITLE": {
    "Tag": "string",
    "TypeCode": "5",
    "Value": "Title of announcement",
    "IsBinaryUnique": true}
}
]

Second: Your request and response have the same structure - ArrayList<AnnouncementTitle>. You are sending this list and getiing it back as an server answer. Is it really the thing you want to do?

Third: If you really want ArrayList<AnnouncementTitle> as an server answer why do you parse it by hand with

for (int i = 0;i<response_array.size();i++){
    ...
}

? It would be better to force Retrofit do that for you with:

call.enqueue(new Callback<ArrayList<AnnouncementTitle>>() {
    @Override
    public void onResponse(Call<ArrayList<AnnouncementTitle>> call, ArrayList<AnnouncementTitle> list) {
        ArrayList<AnnouncementTitle> response_list = list;
    }

Upvotes: 1

Related Questions