我有一个片段需要在其中显示评论API,这是我的接口类
public interface ApiInterface {
@GET("/businesses")
Call<ArrayList<BusinessesResponse>> getMovie();
}
,json响应为:
{
"code": 200,
"long_message": "The request was successful and the response body contains the representation requested.",
"short_message": "Record found successfully",
"data": {
"status": true,
"count": 8,
"businesses": [
{
"id": 30,
"title": "best auto body",
"phone": "4104287884",
"mobile": null,
"address": "3600 Dolfield Avenue, Baltimore, MD, USA",
"lat": "39.3364806",
"lng": "-76.6752669",
"category": "Painting",
"review_ratings": null,
"total_reviews": "0",
"avg_rating": null,
"already_fav": "0",
"image": "http://karobarlist.xtechnos.com/storage/app/public/business/9ICJ2FFTCwmYVWMsL4qAMXf9ZltREs5Ku1JsIrNK.jpeg"
},
{
"id": 11,
"title": "Corrib Moving & Storage",
"phone": null,
"mobile": null,
"address": "Dallas, TX, USA",
"lat": "32.7766642",
"lng": "-96.79698789999998",
"category": "Handyman",
"review_ratings": null,
"total_reviews": "0",
"avg_rating": null,
"already_fav": "1",
"image": "http://karobarlist.xtechnos.com/storage/app/public/business/s8SQG1Mm1gNLd4uOdU59OzFxDy51N7l7RaJtzbgu.png"
},
{
"id": 10,
"title": "NorthStar Moving Company",
"phone": null,
"mobile": null,
"address": "Des Moines International Airport (DSM), Fleur Drive, Des Moines, IA, USA",
"lat": "41.53413330000001",
"lng": "-93.65879580000001",
"category": "Handyman",
"review_ratings": null,
"total_reviews": "0",
"avg_rating": null,
"already_fav": "1",
"image": "http://karobarlist.xtechnos.com/storage/app/public/business/fVeQMzZvikHzzvkKwjkghsRKD7wPGC5D7xnIov9M.jpeg"
},
{
"id": 9,
"title": "Marcio",
"phone": null,
"mobile": null,
"address": "United States Botanic Garden, Maryland Avenue Southwest, Washington, DC, USA",
"lat": "38.8881451",
"lng": "-77.0128833",
"category": "Painting",
"review_ratings": null,
"total_reviews": "0",
"avg_rating": null,
"already_fav": "1",
"image": "http://karobarlist.xtechnos.com/storage/app/public/business/STnuOafrVQUM1z4hBDDUGTc7bnBNiEoZ6DVvQSaM.jpeg"
},
{
"id": 8,
"title": "Fast Response Plumbing & Rooter",
"phone": null,
"mobile": null,
"address": "Des Moines International Airport (DSM), Fleur Drive, Des Moines, IA, USA",
"lat": "41.53413330000001",
"lng": "-93.65879580000001",
"category": "Plumbing",
"review_ratings": null,
"total_reviews": "0",
"avg_rating": null,
"already_fav": "0",
"image": "http://karobarlist.xtechnos.com/storage/app/public/business/qFsye8cOx8kSeJH9kwHAZG2muf2q74BrTHYV9X80.jpeg"
},
{
"id": 7,
"title": "Apollo Heating & Ventilating",
"phone": null,
"mobile": null,
"address": "Square One Shopping Centre, City Centre Drive, Mississauga, ON, Canada",
"lat": "43.5926695",
"lng": "-79.6427339",
"category": "Electricity",
"review_ratings": null,
"total_reviews": "0",
"avg_rating": null,
"already_fav": "1",
"image": "http://karobarlist.xtechnos.com/storage/app/public/business/0NfvQ9689mjksRqw7ThjMGx7y73zyvScwTvEAsCX.jpeg"
},
{
"id": 6,
"title": "Ike’s Electric",
"phone": null,
"mobile": null,
"address": "E.W.R. (EWR), Brewster Road, Newark, NJ, USA",
"lat": "40.68953140000001",
"lng": "-74.17446239999998",
"category": "Electricity",
"review_ratings": "1_5.0000",
"total_reviews": "1",
"avg_rating": "5.0000",
"already_fav": "1",
"image": "http://karobarlist.xtechnos.com/storage/app/public/business/CH6XSVuiOD9rT7pS5XUAO4IPOST89jYtPcfiA9vT.jpeg"
},
{
"id": 3,
"title": "Associated Heating",
"phone": null,
"mobile": null,
"address": "United States Capitol, Washington, DC, USA",
"lat": "38.8899389",
"lng": "-77.0090505",
"category": "A/C",
"review_ratings": null,
"total_reviews": "0",
"avg_rating": null,
"already_fav": "1",
"image": "http://karobarlist.xtechnos.com/storage/app/public/business/2mKtZEP82sNwfZx8UWuRnaDnAbDozWW75npRQ22k.jpeg"
}
]
}
}
我在片段中遇到错误:
public void getBusinessList() {
businessesList = new ArrayList<>();
ApiInterface apiInterface = NetworkClient.getRetrofitClient().create(ApiInterface.class);
Call<ArrayList<BusinessesResponse>> call = apiInterface.getMovie();
call.enqueue(new Callback<ArrayList<BusinessesResponse>>() {
@Override
public void onResponse(Call<ArrayList<BusinessesResponse>> call, Response<ArrayList<BusinessesResponse>> response) {
if(response.isSuccessful()) {
Log.d("MainActivity", "posts loaded from API");
}else {
int statusCode = response.code();
}
if (response==null){
Toast.makeText(getActivity(), "Somthing Went Wrong...!!", Toast.LENGTH_SHORT).show();
} else if (response.body()==null){
Toast.makeText(getActivity(), "Something Went Wrong. List is empty!!", Toast.LENGTH_SHORT).show();
}else{
businessesList.addAll(response.body());
Log.i("RESPONSE: ", ""+response.toString());
}
productAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<ArrayList<BusinessesResponse>> call, Throwable t) {
Toast.makeText(getActivity(), "Unable to fetch json: " + t.getMessage(), Toast.LENGTH_LONG).show();
Log.e("ERROR: ", t.getMessage());
}
});
}
这是我在线生成的模型类:
public class BusinessesResponse {
@SerializedName("code")
@Expose
private Integer code;
@SerializedName("long_message")
@Expose
private String longMessage;
@SerializedName("short_message")
@Expose
private String shortMessage;
@SerializedName("data")
@Expose
private Data data;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getLongMessage() {
return longMessage;
}
public void setLongMessage(String longMessage) {
this.longMessage = longMessage;
}
public String getShortMessage() {
return shortMessage;
}
public void setShortMessage(String shortMessage) {
this.shortMessage = shortMessage;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
}
秒是:
import com.google.gson.annotations.Expose;
导入com.google.gson.annotations.SerializedName;
公共类企业{
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("phone")
@Expose
private Object phone;
@SerializedName("mobile")
@Expose
private Object mobile;
@SerializedName("address")
@Expose
private String address;
@SerializedName("lat")
@Expose
private String lat;
@SerializedName("lng")
@Expose
private String lng;
@SerializedName("category")
@Expose
private String category;
@SerializedName("review_ratings")
@Expose
private Object reviewRatings;
@SerializedName("total_reviews")
@Expose
private String totalReviews;
@SerializedName("avg_rating")
@Expose
private Object avgRating;
@SerializedName("already_fav")
@Expose
private String alreadyFav;
@SerializedName("image")
@Expose
private String image;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Object getPhone() {
return phone;
}
public void setPhone(Object phone) {
this.phone = phone;
}
public Object getMobile() {
return mobile;
}
public void setMobile(Object mobile) {
this.mobile = mobile;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getLng() {
return lng;
}
public void setLng(String lng) {
this.lng = lng;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public Object getReviewRatings() {
return reviewRatings;
}
public void setReviewRatings(Object reviewRatings) {
this.reviewRatings = reviewRatings;
}
public String getTotalReviews() {
return totalReviews;
}
public void setTotalReviews(String totalReviews) {
this.totalReviews = totalReviews;
}
public Object getAvgRating() {
return avgRating;
}
public void setAvgRating(Object avgRating) {
this.avgRating = avgRating;
}
public String getAlreadyFav() {
return alreadyFav;
}
public void setAlreadyFav(String alreadyFav) {
this.alreadyFav = alreadyFav;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
第三个模型类是:
public class Data {
@SerializedName("status")
@Expose
private Boolean status;
@SerializedName("count")
@Expose
private Integer count;
@SerializedName("businesses")
@Expose
private List<Business> businesses = null;
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public List<Business> getBusinesses() {
return businesses;
}
public void setBusinesses(List<Business> businesses) {
this.businesses = businesses;
}
}