Prabhakaran
Prabhakaran

Reputation: 73

How to fetch Json data by using retrofit?

I am new to retrofit. I need to fetch the below json data. When i run my application java.lang.ClassCastException: ad.app.retrofitexample.model.DS cannot be cast to retrofit2.Call

This type of exception is occurring. Please help me how to solve this. I explained my code in detail description in below.

 {
    "DS": {
        "LST": [
            {
                "BID": 3,
                "CNM": "kumar",
                "BNO": "BLO41",
                "VID": 1,
                "ETI": 5,
                "FRM": "/Date(1512930840000)/",
                "TOD": "/Date(1513017240000)/",
                "ENM": "Birthday Party",
                "VNM": "murugan hall",
                "CNO": "8888888888",
                "AD1": "ram nagar",
                "AD2": "ram colony",
                "AD3": "polachi",
                "CTN": "Coimbatore",
                "CDT": "/Date(1512799571000)/",
                "ICN": "0",
                "SDT": "10/12/2017",
                "EDT": "11/12/2017"
            }
        ]
    }
}

For this i used pojo class

public class DS {
    public DSObj getDsObj() {
        return dsObj;
    }

    public void setDsObj(DSObj dsObj) {
        this.dsObj = dsObj;
    }

    @SerializedName("DS")
    private DSObj dsObj;

}

The above model class is for accessing json object "DS" .

  public class DSObj {

@SerializedName("LST")
    private List<Booking> list;

    public List<Booking> getList() {
        return list;
    }

    public void setList(List<Booking> list) {
        this.list = list;
    }
}

The above class DSObj is accessing the inner "DS" json object.

    public class Booking {

    @SerializedName("CNM")
    String customerName;
    @SerializedName("BNO")
    String bookingNumber;
    @SerializedName("CNO")
    String contactNumber;
    @SerializedName("VNM")
    String venueName;
    @SerializedName("CTN")
    String cityName;
    @SerializedName("VID")
    String vid;

    public String getVenueName() {
        return venueName;
    }

    public void setVenueName(String venueName) {
        this.venueName = venueName;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getBookingNumber() {
        return bookingNumber;
    }

    public void setBookingNumber(String bookingNumber) {
        this.bookingNumber = bookingNumber;
    }

    public String getContactNumber() {
        return contactNumber;
    }

    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }


    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }


    public String getVid() {
        return vid;
    }

    public void setVid(String vid) {
        this.vid = vid;
    }
}

The above model class is for accessing the inner "LST" json array.

The Api Interface class for retrofit is Kpublic interface ApiInterface {

    String base_url = "http://195.100.1.103/mswed";
            // listVenue
   @GET("/?api=listBlocking&eid=5&vid=1&fdt=9/11/2017&tdt=9/01/2018&uid=1")
    Call<DS> getVenueData();
}

The below code is initializing the retrofit and getting the data.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bookingList = (ListView)findViewById(R.id.listData);

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(ApiInterface.base_url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    ApiInterface apiInterface = retrofit.create(ApiInterface.class);
    Call<DS> call = apiInterface.getVenueData();
    call.enqueue(new Callback<DS>() {
        @Override
        public void onResponse(Call<DS> call, Response<DS> response) {

            Call<DS> getList = (Call<DS>) response.body();

            Log.e("Data",getList+"");

        }

        @Override
        public void onFailure(Call<DS> call, Throwable t) {
            Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();

            Log.e("Json Data Error",t.getMessage());
        }
    });

}

When i run the application exception is thrown , i tried but i can't found solution .

Upvotes: 2

Views: 1534

Answers (3)

Mr. Programmer
Mr. Programmer

Reputation: 14

List mList= new ArrayList(); mList.addAll(response.body().getDsObj())

get from list

mList.get(position).getCustomerName.toString()

Upvotes: 0

Bhoomika Patel
Bhoomika Patel

Reputation: 1925

For getting Perticular String from Given Response, You Should Try this.

DS dsList= response.body().getDsObj();
List<Booking> mList= dsList.getList();

Here in mList you get Array from Response, now you can use this however you want to use.

Upvotes: 3

KeLiuyue
KeLiuyue

Reputation: 8237

Change

Call<DS> getList = (Call<DS>) response.body();

to

DS getList = response.body();

Upvotes: 1

Related Questions