When a feature image is empty, my Android app is f

2019-08-20 21:48发布

问题:

I am trying to create JSON REST API app in Android. The REST API has call from WordPress REST API. So I have shown the WordPress posts (title, feature image, description) in Android ListView with image and I have done this work. But I faced one problem, when many posts does not have feature images then while running the app, the app has stopped.

Here is my code:

MainActivity.class code;
this class has set the JSON REST API with model.

public void getRetrofit(){

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(baseURL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

RetrofitArrayApi service = retrofit.create(RetrofitArrayApi.class);
Call<List<WPPost>>  call = service.getPostInfo();

call.enqueue(new Callback<List<WPPost>>() {
    @Override
    public void onResponse(Call<List<WPPost>> call, Response<List<WPPost>> response) {
        Log.e("mainactivity", " response "+ response.body());
        mListPost = response.body();
        progressBar.setVisibility(View.GONE);
        for (int i=0; i<response.body().size();i++){
            Log.e("main ", " title "+ response.body().get(i).getTitle().getRendered() + " "+
                    response.body().get(i).getId());



            list.add( new Model( Model.IMAGE_TYPE, response.body().get(i).getId().toString(),  response.body().get(i).getTitle().getRendered(),
                    response.body().get(i).getExcerpt().getRendered().toString(),
                    response.body().get(i).getBetterFeaturedImage().getSourceUrl() )  );


        }
        adapter.notifyDataSetChanged();

    }

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

    }
});



}

Adapter.class code
I have set the image, the image has shown when all post have feature image, but any post has not set image this time app has stop, the app has not working. I have set if else loop, this loop not work.

if(object.Image !=null && !object.Image.equals("")) {

            Glide.with(mContext)
                    .load(object.Image)
                    .placeholder(R.drawable.ic_launcher_background)
                    .error(R.drawable.ic_launcher_background)
                    .into(((ImageTypeViewHolder) holder).imageView);

        }else {

            Glide.with(mContext)
                    .load(R.drawable.ic_launcher_foreground)
                    .placeholder(R.drawable.ic_launcher_background)
                    .error(R.drawable.ic_launcher_background)
                    .into(((ImageTypeViewHolder) holder).imageView);

        }

Model.class
this model class for wordpress post

public class Model {

    public static final int IMAGE_TYPE =1;  
    public String id, title, subtitle, Image ;
    public int type;


    public Model ( int mtype, String mid, String mtitle, String msubtitle, String image  ){
        this.id=mid;
        this.title = mtitle;
        this.subtitle = msubtitle;
        this.type = mtype;
        this.Image = image;

    }
    public String getId() {
        return this.id;
    }

    public String getTitle() {
        return this.title;
    }

    public String getSubtitle() {
        return this.subtitle;
    }


}