当功能图片为空时,我的Android应用被强制停止

时间:2018-07-28 19:03:59

标签: android json retrofit retrofit2 wordpress-rest-api

我正在尝试在Android中创建JSON REST API应用。 REST API已从WordPress REST API进行调用。因此,我在Android ListView中显示了带图片的WordPress帖子(标题,功能图片,描述),并完成了这项工作。但是我遇到了一个问题,当许多帖子没有功能图片,那么在运行该应用程序时,该应用程序已停止。

这是我的代码:

MainActivity.class代码;
此类设置了带有模型的JSON REST API。

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代码
 我已设置图像,当所有帖子都具有功能图像时该图像已显示,但是这次应用停止时任何帖子都未设置图像,该应用无法正常工作。我设置了if else循环,此循环不起作用。

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
 这个用于WordPress发布的模型类

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


}

0 个答案:

没有答案