简体   繁体   中英

Data from server is not displaying on screen

So am working with retrofit and rxjava for my application.so am using the @GET annociation to pull my blog details from the server that include blog_title, blog_content, blog_thumbnail etc and all this parameter are within an array called blog_post. I have my APIClient:

public class ApiClient {

 private static final String STAGING_BASE_URL = "https://watchnollywood.ml/api/";
 private static ApiClient instance;
 private ApiService apiService;

 private ApiClient(){
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    // set your desired log level
    // TODO: 21/03/2017 when going live change the log level to NONE, to enhance performance
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    // add logging as last interceptor
    httpClient.addInterceptor(logging);  // <-- this is the important line for logging requests!

    final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
    //final Retrofit retrofit = new Retrofit.Builder().baseUrl(STAGING_BASE_URL).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create(gson)).client(httpClient.build()).build();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(STAGING_BASE_URL)
            .client(httpClient.build())
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();
    apiService = retrofit.create(ApiService.class);
}

public static ApiClient getInstance(){
    if(instance == null){
        instance = new ApiClient();

    }
    return instance;
}

//API CALL FOR LOGIN
public Observable<UserItem> login(String email, String password){
    return apiService.signIn(email,password);
}
//API CALL FOR SIGNUP
public Observable<StatusItem> signup(String email, String password, String full_name, String phone){
    return apiService.signUp(email, password,phone,full_name);
}
//API CALL FOR BLOG DETAILS
public Observable<BlogResponse> blog_post(){
    return apiService.blog_post();
 }
 }

ApiService:

public interface ApiService {
@FormUrlEncoded
@POST("signin")
Observable<UserItem> signIn(@Field("email") String email, @Field("password") String password);

@FormUrlEncoded
@POST("signup")
Observable<StatusItem> signUp(@Field("full_name")String full_name, @Field("phone") String phone, @Field("email") String email, @Field("password") String password);


@GET("blog")
Observable<BlogResponse> blog_post();

}

pojo classes:

public class BlogItem {
private int thumbNail;
private String title;
private String summary;

public int getThumbNail() {
    return thumbNail;
}

public void setThumbNail(@DrawableRes int thumbNail) {
    this.thumbNail = thumbNail;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getSummary() {
    return summary;
}

public void setSummary(String summary) {
    this.summary = summary;
}

}

public class BlogResponse {
private BlogItem[] blogItems;

public BlogItem[] getBlogItems() {
    return blogItems;
}

public void setBlogItems(BlogItem[] blogItems) {
    this.blogItems = blogItems;
}
}

I have a recyclerview that will hold all the information that will be coming from the server. But the problem is that when I run it I get a log response in my RUN terminal but nothing is showing on the app screen. this is my FragmentClass that holds the information from the server: BlogFragment:

public class BlogFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";

private String mParam1;
private RecyclerView recyclerView;
private BlogAdapter adapter;
private List<BlogItem> blogItems;
private View view;


public BlogFragment() {
    // Required empty public constructor
}

public static BlogFragment newInstance(String param1) {
    BlogFragment fragment = new BlogFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_blog, container, false);
    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    recyclerView = (RecyclerView) view.findViewById(R.id.blog_posts_list);
    setUpViews();
}

private void setUpViews() {
    blogItems = new ArrayList<>();
    adapter = new BlogAdapter(blogItems);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
    BlogPost();
  //  populateLists();
}

private void BlogPost() {
    ApiClient.getInstance().blog_post().observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.newThread()).subscribe(new DisposableObserver<BlogResponse>() {
        @Override
        public void onNext(BlogResponse value) {
            BlogItem blogItem = new BlogItem();
            blogItem.setTitle(blogItem.getTitle().toString());
            blogItem.setSummary(blogItem.getSummary().toString());

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onComplete() {
            BlogItem blogItem = new BlogItem();
            blogItem.setTitle("blog_title");
            blogItem.setSummary("blog_content");


        }
    });
    adapter.notifyItemRangeChanged(0, adapter.getItemCount());
}
/*
private void populateLists() {

    int dummyPostArraySize = 10;
    for (int i = 0; i < dummyPostArraySize; i++) {
        BlogItem blogItem = new BlogItem();
        blogItem.setTitle("Post title " + i+1);
        blogItem.setThumbNail(isEven(i) ? R.drawable.profile_image : 0);
        blogItem.setSummary(getString(isEven(i) ? R.string.summary2 : R.string.summary1));
        blogItems.add(blogItem);
    }
    adapter.notifyItemRangeChanged(0, adapter.getItemCount());
}*/
private boolean isEven(int position) {
    return (position & 1) == 0;
}
}

Adapter class

public class BlogAdapter extends RecyclerView.Adapter<BlogViewHolder> {
private List<BlogItem> blogItems;

public BlogAdapter(List<BlogItem> blogItems) {
    this.blogItems = blogItems;
}

@Override
public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.blog_post_item,
            parent, false);
    return new BlogViewHolder(view);
}

@Override
public void onBindViewHolder(BlogViewHolder holder, int position) {
    BlogItem blogItem = blogItems.get(position);
    holder.bindModel(blogItem);
}

@Override
public int getItemCount() {
    return  blogItems == null ? (0) : blogItems.size();
}
}

View Holder class

public class BlogViewHolder extends RecyclerView.ViewHolder {
private ImageView cover;
private TextView title;
private TextView summary;

public BlogViewHolder(View itemView) {
    super(itemView);
    cover = (ImageView) itemView.findViewById(R.id.post_thumbnail);
    title = (TextView) itemView.findViewById(R.id.post_title);
    summary = (TextView) itemView.findViewById(R.id.post_summary);
}

public void bindModel(BlogItem blogItem) {
    if (blogItem.getThumbNail() == 0) {
        cover.setVisibility(View.GONE);
    } else {
        cover.setImageResource(blogItem.getThumbNail());
    }
    title.setText(Html.fromHtml(blogItem.getTitle()));
    summary.setText(blogItem.getSummary());
}
}

What am I not doing right. Someone Please Help!!!

In the private void BlogPost() { method, you create BlogItem s but then don't do anything with it. You probably forgot to add them to the blogItems list.

In addition, the call to adapter.notifyItemRangeChanged in that method happens way before the sequence receives data but you don't call that after each blogItem or when all blog items have arrived - the observer is on a complete different execution path than the outer BlogPost() method.

Edit spelled out:

    @Override
    public void onNext(BlogResponse value) {
        for (BlogItem responseItem : value.getBlogItems()) {
            BlogItem blogItem = new BlogItem();
            blogItem.setTitle(responseItem.getTitle().toString());
            blogItem.setSummary(responseItem.getSummary().toString());

            blogItems.add(blogItem);
        }
        adapter.notifyItemRangeChanged(0, adapter.getItemCount());
    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public void onComplete() {
        BlogItem blogItem = new BlogItem();
        blogItem.setTitle("blog_title");
        blogItem.setSummary("blog_content");

        blogItems.add(blogItem);
        adapter.notifyItemRangeChanged(0, adapter.getItemCount());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM