Retrofit 2 - 响应体给出null值

时间:2017-09-13 06:48:50

标签: android retrofit retrofit2

我想从Web服务检索数据到textview。 Web服务运行良好。我正在使用网络改造。但我在prmoDetails中得到null response.body。我已经检查并尝试了所有过去的解决方案。但它还没有奏效。请帮我解决这个问题。

POJO班

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界面

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) {

        }
    });


}}

我这样的网络服务

[{"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"}]

4 个答案:

答案 0 :(得分:2)

我认为这是因为你的pojo类变量与你的响应变量不匹配。使用@SerializedName注释或基本上更改您的pojo定义,就像在响应中一样。例如:

@SerializedName("promoId")
private String PromoId;

@SerializedName("pName")
private String PromoName;

private String promoId;
private String pName;

答案 1 :(得分:1)

  

由于您没有将任何数据传递给getPromotions,我认为它是一个get方法。在代码中进行以下更改

    public interface ApiInterface {

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

并将您的pojo类更改为以下

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;
}

}

答案 2 :(得分:0)

为什么你的post方法没有任何传递参数???

检查方法,是 GET 还是 POST

如果是GET, 然后将其更改为@GET("ap/promotions.php"),其他代码将相同。

答案 3 :(得分:0)

在获取正文之前检查response.isSuccessful(),如果是false,则获取response.errorBody(),这将显示错误原因