retrofit在日志中显示响应,但片段出现空白

时间:2016-08-15 12:15:27

标签: json listview retrofit2

我已经尝试了几乎所有的解决方案,但仍然无法解决问题。响应正在日志中显示,但它仍然显示一个空白片段。也许我没有正确解析它?我该怎么办?那? JSON响应:

[
  {
    "place_id": "1",
    "stop_id": "0",
    "place_name": "Udon House",
    "category": "food",
    "rating": "3",
    "latitude": "31.5076701",
    "longitude": "74.3544522",
    "description": "a trip to the landa (Flea market of Lahore), tea time and chess with neighbors and a date at Udon House with Nida. I really need that heart to heart session with her. Too much to be unloaded off the chest and  Udon House is the perfect place to do that. At least for us.",
    "db_image": "images/udonHouse.jpg"
  },
  {
    "place_id": "5",
    "stop_id": "0",
    "place_name": "Lahore Meuseum",
    "category": "attractions",
    "rating": "4",
    "latitude": "31.5681556",
    "longitude": "74.3061492",
    "description": "want to discover histroy?",
    "db_image": "images/badshahi.jpg"
  },
  {
    "place_id": "4",
    "stop_id": "0",
    "place_name": "Pearl Continental ",
    "category": "hotels",
    "rating": "2",
    "latitude": "31.552531",
    "longitude": "74.3359265",
    "description": "hotel,pool and everything you need.",
    "db_image": "images/1.jpg"
  },
  {
    "place_id": "6",
    "stop_id": "0",
    "place_name": "Shalimar Gardens",
    "category": "attractions",
    "rating": "2",
    "latitude": "31.586937",
    "longitude": "74.3780747",
    "description": "The city of Lahore is often referred to as the City of Gardens for the reason that many a beautiful garden of ancient and modern times and architecture are found all over the city. Successive Mogul emperors constructed a number of gardens to add to the beauty of Lahore – a city they loved the most.",
    "db_image": "images/1.jpg"
  },
  {
    "place_id": "2",
    "stop_id": "0",
    "place_name": "Casa Biance",
    "category": "food",
    "rating": "4",
    "latitude": "31.5108486",
    "longitude": "74.3459973",
    "description": "A varity of seafood and vegetable fried in a homemade tempura batter and served with tartar sauce",
    "db_image": "images/e1c6e8964483782e30ee09b92191c544.jpg"
  },
  {
    "place_id": "3",
    "stop_id": "0",
    "place_name": "Pizza hut",
    "category": "food",
    "rating": "5",
    "latitude": "31.4622413",
    "longitude": "74.2732879",
    "description": "Order pizza online for fast pizza delivery or drop by for carryout. You may also contact Pizza Hut and find out about our catering services for your next big event.",
    "db_image": "images/pizza2lahore.jpg"
  }
]

接口:

public interface fav_interface {
    @FormUrlEncoded
    @POST("getFavorites.php")
    Call<List<FavoritesModel>> getFavs(@Field("user_id") int user_id);
}

模型:

public class FavoritesModel {

    @Expose
    @SerializedName("place_id")
    int  place_id;

    @Expose
    @SerializedName("place_name")
    String Place_name;

    @Expose
    @SerializedName("latitude")
    Double latitude;

    @Expose
    @SerializedName("longitude")
    Double logitude;

    @Expose
    @SerializedName("category")
    String category;

    @Expose
    @SerializedName( "description")
    String description;
    @Expose
    @SerializedName("db_image")
    String db_image;

    FavoritesModel(int place_id, String place_name, String img, String des){
        this.place_id = place_id;
        this.Place_name = place_name;
        this.db_image = img;
        this.description = des;
    }
....

适配器:

public class favAdapter  extends RecyclerView.Adapter<favAdapter.ViewHolder> {
  place_details place_details;

    Constants constant;
    private Context context;
    private List<FavoritesModel> places_list;
    FavoritesModel favoritesModel;


    public favAdapter(Context context, List<FavoritesModel> places) {
        this.context = context;
        this.places_list = places;

    }


    @Override
    public favAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        final View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_item, viewGroup, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(favAdapter.ViewHolder holder, int position) {
        places_list.get(position);

        holder.place_name.setText(places_list.get(position).getPlace_name());

        //.placeholder(R.color.colorAccent)
        Glide.with(context).load(constant.BASE_URL + places_list.get(position).getPlace_name()).into(holder.place_image);


    }


    @Override
    public int getItemCount() {
        if (places_list != null) {
            return places_list.size();
        }
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private static final String TAG = "position";
        private TextView place_name, place_category;
        ImageView place_image, Bucket, like;

        public ViewHolder(View view) {
            super(view);

            place_name = (TextView) view.findViewById(R.id.place_name);
            place_image = (ImageView) view.findViewById(R.id.place_image);


            place_image.setOnClickListener(this);
            place_name.setOnClickListener(this);


        }


        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.place_name: {
....
}

1 个答案:

答案 0 :(得分:1)

您可以通过JsonElement接收响应,并通过任何方法将其反序列化到您的List。

@FormUrlEncoded
    @POST("getFavorites.php")
    Call<JsonElement> getFavs(@Field("user_id") int user_id);

响应:

call.enqueue(new Callback<JsonElement>() {
        @Override
        public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
            if(response.isSuccessful()){
                JsonElement jsonElement = response.body();
                JsonObject objectWhichYouNeed = jsonElement.getAsJsonObject();
                //do whatever you want with Json Object. Deserialize it, get values by keys, get as String etc.
            }
            else{
                System.out.println(response.message());
            }
        }
        @Override
        public void onFailure(Call<JsonElement> call, Throwable t) {
            System.out.println("Failed");
        }
    });