0

I want to retrieve data from a web service to textview. Web service works well. I'm using retrofit for network. But I'm getting null in prmoDetails here response.body. I have checked and tried all past solutions as well. But still it's not working. Please help me to solve this.

POJO class

public class PromoDetails {



private String PromoId;

private String PromoName;

private String Category;

private String PromoImg;

private String promoDetails;

private String promoValidty;

public PromoDetails(String PromoId, String PromoName, String Category , String PromoImg , String promoDetails , String promoValidity) {
    this.PromoId = PromoId;
    this.PromoName = PromoName;
    this.Category = Category;
    this.PromoImg = PromoImg;
    this.promoDetails = promoDetails;
    this.promoValidty = promoValidity;
}


public String getPromoId() {
    return PromoId;
}

public void setPromoId(String promoId) {
    PromoId = promoId;
}

public String getPromoName() {
    return PromoName;
}

public void setPromoName(String promoName) {
    PromoName = promoName;
}

public String getCategory() {
    return Category;
}

public void setCategory(String category) {
    Category = category;
}

public String getPromoImg() {
    return PromoImg;
}

public void setPromoImg(String promoImg) {
    PromoImg = promoImg;
}

public String getPromoDetails() {
    return promoDetails;
}

public void setPromoDetails(String promoDetails) {
    this.promoDetails = promoDetails;
}

public String getPromoValidty() {
    return promoValidty;
}

public void setPromoValidty(String promoValidty) {
    this.promoValidty = promoValidty;
}}

Api Interface

public interface ApiInterface {

@POST("ap/promotions.php")
Call<List<PromoDetails>> getPromotions();}

MainActivity

public class MainActivity extends AppCompatActivity {

private ApiInterface apiInterface;
private List<PromoDetails> promoDetails;


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

    getPromotionUpdate();

}

private void getPromotionUpdate() {


    apiInterface = ApiClient.getApiClient().create(ApiInterface.class);


    Call<List<PromoDetails>> call = apiInterface.getPromotions();
    call.enqueue(new Callback<List<PromoDetails>>() {
        @Override
        public void onResponse(Call<List<PromoDetails>> call, Response<List<PromoDetails>> response) {
            promoDetails = response.body();

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    TextView prDescription = (TextView)findViewById(R.id.TextView1) ;
                    prDescription.setText(promoDetails.get(0).getPromoId());
                }
            });


        }

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

        }
    });


}}

My web service like this

[{"promoId":"7","companyName":"Pizza Hut","pName":"Connecting Dots TEDXCOLOMBO 2017","category":"EVENTS","pImg":"https:\/\/androidsra.000webhostapp.com\/img_ap\/tedx.png"},{"promoId":"6","companyName":"Subway","pName":"BUY any SUB & get another SUB FREE!","category":"FOODS & DRINKS","pImg":"https:\/\/androidsra.000webhostapp.com\/img_ap\/subway.png"},{"promoId":"5","companyName":"KFC ","pName":"40% off at Queens Hotel - Kandy for HSBC Credit cards.","category":"BANKS & CARDS","pImg":"https:\/\/androidsra.000webhostapp.com\/img_ap\/queens.png"},{"promoId":"4","companyName":"Pizza Hut","pName":"New sets of Furniture with special discounts.","category":"HOME & KITCHEN","pImg":"https:\/\/androidsra.000webhostapp.com\/img_ap\/singerfur_promo.png"},{"promoId":"3","companyName":"Browns Tours","pName":"Exclusive Offer !! Fly to Melbourne with Srilankan Airlines from Browns Tours","category":"TRAVEL","pImg":"https:\/\/androidsra.000webhostapp.com\/img_ap\/melbourne_promo.png"},{"promoId":"2","companyName":"KFC ","pName":"Hot Drumlets with 2L Pepsi for just Rs.1100\/- only","category":"FOODS & DRINKS","pImg":"https:\/\/androidsra.000webhostapp.com\/img_ap\/kfc_promo.png"},{"promoId":"1","companyName":"Pizza Hut","pName":"50% Off for Medium Pizzas.","category":"FOODS & DRINKS","pImg":"https:\/\/androidsra.000webhostapp.com\/img_ap\/pizza_promo.png"}]
venura
  • 125
  • 2
  • 15

4 Answers4

2

I think it's because your pojo class variables doesn't match with your response variables. Either use @SerializedName annotation or basically change your pojo definitions as exactly as in your response. For example:

@SerializedName("promoId")
private String PromoId;

@SerializedName("pName")
private String PromoName;

or

private String promoId;
private String pName;
Fatih Santalu
  • 4,641
  • 2
  • 17
  • 34
1

As you are not passing any data to getPromotions i presume its a get Method. make following changes in code

    public interface ApiInterface {

  change --------> @GET("ap/promotions.php")
    Call<List<PromoDetails>> getPromotions();}

and change your pojo class to following

public class Example {

@SerializedName("promoId")
@Expose
private String promoId;
@SerializedName("companyName")
@Expose
private String companyName;
@SerializedName("pName")
@Expose
private String pName;
@SerializedName("category")
@Expose
private String category;
@SerializedName("pImg")
@Expose
private String pImg;

public String getPromoId() {
return promoId;
}

public void setPromoId(String promoId) {
this.promoId = promoId;
}

public String getCompanyName() {
return companyName;
}

public void setCompanyName(String companyName) {
this.companyName = companyName;
}

public String getPName() {
return pName;
}

public void setPName(String pName) {
this.pName = pName;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public String getPImg() {
return pImg;
}

public void setPImg(String pImg) {
this.pImg = pImg;
}

}
akshay_shahane
  • 4,423
  • 2
  • 17
  • 30
  • Yes. I changed it. But same issue – venura Sep 13 '17 at 06:58
  • Thank you so much. Can you please help me with this ? https://stackoverflow.com/questions/46177484/how-to-retrieve-web-service-values-through-retrofit-to-text-view?noredirect=1#comment79320108_46177484 – venura Sep 13 '17 at 07:20
0

why your post method don't have any passing parameters???

Check method, is it GET or POST?

If it is GET, then change it to @GET("ap/promotions.php") , other code will be same.

zephyr
  • 665
  • 6
  • 19
0

check response.isSuccessful() before getting the body, if it is false get response.errorBody() and that will show you the cause of error

Ramees Thattarath
  • 1,093
  • 11
  • 19