Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the Retrofit response gives null value for fields, while the response can get the object?

I'm just making a simple application in Android Studio 2.3 to get a JSON response from the Web app. I chose Retrofit 2.0 for consuming the response.


It seems I could get the objects inside onResponse

Object item =  response.body().get(1);
Log.e("success", String.valueOf(item));

inside logcat it gives a success,

success:com.makemyapp.firstapplication.Item@67205ae

But when tried to fetch the fields inside the object,

Object item =  response.body().get(1).getItemDescription();
Log.e("success", String.valueOf(item));

It returns a null

success: null

This is the Interface- GrocsApi,

package com.makemyapp.firstapplication;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;

public interface GrocsApi {

    @GET("api/getitemlist")
    Call<List<Item>> getItem();

}

Class for returning service- GrocsService

package com.makemyapp.firstapplication;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class GrocsService {

    public  static GrocsApi service;
    public static String BASE_URL = "http://10.0.2.2:8000/";


    public static GrocsApi getGrocsService(){
        if (service==null){

            Retrofit retrofit= new Retrofit.Builder()
                              .baseUrl(BASE_URL)
                              .addConverterFactory(GsonConverterFactory.create())
                              .build();
            service = retrofit.create(GrocsApi.class);

            return service;
        }

return  service;
    }

}

MainActivity.java ,where Request Call is initiated,

package com.makemyapp.firstapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class MainActivity extends AppCompatActivity {


    Button button;
    //TextView textView; for later use


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


    public void onClickGetData(){

      //  textView=(TextView)findViewById(R.id.textView); for later use
        button=(Button)findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
  Call<List<Item>> call =   GrocsService.getGrocsService().getItem();

           call.enqueue(new Callback<List<Item>>  () {
                  @Override
                  public void onResponse(Call<List<Item>>  call, Response<List<Item>>   response) {

 if(response.isSuccessful()) {
     Object item =  response.body().get(1).getItemDescription();

     Log.e("success", String.valueOf(item));

 } else{
     Log.e("response is not good", "");
 }
                  }

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

                      Log.e("failed", t.getMessage());

                  }
              });


            }

        }
    );
}
}

This is my POJO class- Item

package com.makemyapp.firstapplication;


public class Item {

    private Integer itemId;
    private Object itemName;
    private Object itemDescription;
    private Object itemPhotoUrl;
    private Object itemWikipediaLink;
    private Object itemTag;

    public Integer getItemId() {
        return itemId;
    }

    public void setItemId(Integer itemId) {
        this.itemId = itemId;
    }

    public Object getItemName() {
        return itemName;
    }

    public void setItemName(Object itemName) {
        this.itemName = itemName;
    }

    public Object getItemDescription() {
        return itemDescription;
    }

    public void setItemDescription(Object itemDescription) {
        this.itemDescription = itemDescription;
    }

    public Object getItemPhotoUrl() {
        return itemPhotoUrl;
    }

    public void setItemPhotoUrl(Object itemPhotoUrl) {
        this.itemPhotoUrl = itemPhotoUrl;
    }

    public Object getItemWikipediaLink() {
        return itemWikipediaLink;
    }

    public void setItemWikipediaLink(Object itemWikipediaLink) {
        this.itemWikipediaLink = itemWikipediaLink;
    }

    public Object getItemTag() {
        return itemTag;
    }

    public void setItemTag(Object itemTag) {
        this.itemTag = itemTag;
    }

}

And the JSON response is ,

[{"ItemId":1,"ItemName":"Orange","ItemDescription":"Sweet and Juicy","ItemPhotoUrl":"https:\/\/en.wikipedia.org\/wiki\/Orange_(fruit)","ItemWikipediaLink":"'https:\/\/en.wikipedia.org\/wiki\/Orange_(fruit)#\/media\/File:Orange-Whole-%26-Split.jpg","ItemTag":"RS.70 per kg"},{"ItemId":3,"ItemName":"Apple","ItemDescription":"Reddish","ItemPhotoUrl":"https:\/\/en.wikipedia.org\/wiki\/Apple","ItemWikipediaLink":"https:\/\/en.wikipedia.org\/wiki\/Apple#\/media\/File:Red_Apple.jpg","ItemTag":"RS.10 per kg"},{"ItemId":4,"ItemName":"Mango","ItemDescription":"Fresh","ItemPhotoUrl":"https:\/\/en.wikipedia.org\/wiki\/Mango#\/media\/File:Mangoes_pic.jpg","ItemWikipediaLink":"https:\/\/en.wikipedia.org\/wiki\/Mango","ItemTag":"RS.100 per kg"},{"ItemId":5,"ItemName":"Pineapple","ItemDescription":"Large and Juicy","ItemPhotoUrl":"https:\/\/en.wikipedia.org\/wiki\/Pineapple#\/media\/File:Pineapple_and_cross_section.jpg","ItemWikipediaLink":"https:\/\/en.wikipedia.org\/wiki\/Pineapple","ItemTag":"RS.50 per kg"},{"ItemId":6,"ItemName":"Grapes","ItemDescription":"Dark and Juicy","ItemPhotoUrl":"https:\/\/en.wikipedia.org\/wiki\/Grape#\/media\/File:Abhar-iran.JPG","ItemWikipediaLink":"https:\/\/en.wikipedia.org\/wiki\/Grape","ItemTag":"110 per kg"},{"ItemId":7,"ItemName":"Guava","ItemDescription":"Green","ItemPhotoUrl":null,"ItemWikipediaLink":null,"ItemTag":"50 per kg"},{"ItemId":8,"ItemName":null,"ItemDescription":null,"ItemPhotoUrl":null,"ItemWikipediaLink":null,"ItemTag":null}]

Could any one spot a light on my problem?

like image 418
Maa Avatar asked Jan 29 '23 05:01

Maa


1 Answers

Just add @SerializedName on each field of Item class with the JsonObject name that will fill this field.

for example:

@SerializedName("ItemId") // Case sensitive
private Integer itemId;

In your MainActivity:

Call<List<Item>> call =   GrocsService.getGrocsService().getItem(); //It's here where you call getItem(). you will enque the request after that as you already did.

than in your onResponse callback, you get your list:

List<item> myResponse =  (List<Item>) response.body();

and than you fetch your list of items.

like image 139
Fakher Avatar answered Jan 31 '23 19:01

Fakher