Reputation: 3271
I am trying to fetch data from server and showing it in recycler view.I am using retrofit library and RxJava2 but its unable to fetch data from server.
Its showing following line in LogCat:
E/RecyclerView: No adapter attached; skipping layout
Response form the server:
[
{
"term_id": "4",
"name": "Entertainment"
},
{
"term_id": "5",
"name": "Tech & Gadgets"
},
{
"term_id": "6",
"name": "Sports"
},
{
"term_id": "7",
"name": "Health and Fitness Tips"
}
]
Below is my code:
RetrofitClient.java
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getInstance(){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(22,TimeUnit.SECONDS)
.readTimeout(22, TimeUnit.SECONDS)
.writeTimeout(22, TimeUnit.SECONDS)
.build();
if(retrofit == null)
retrofit = new Retrofit.Builder()
.baseUrl("https://www.flypped.com/api/")
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
return retrofit;
}
}
ApiService.class
public interface ApiService {
@GET("Categoery_api")
Observable<List<Model>> getData();
}
Model.java
public class Model {
@SerializedName("catId")
@Expose
String catId;
@SerializedName("catName")
@Expose
String catName;
public Model(){
}
public Model(String catId, String catName) {
this.catId = catId;
this.catName = catName;
}
public String getCatId() {
return catId;
}
public void setCatId(String catId) {
this.catId = catId;
}
public String getCatName() {
return catName;
}
public void setCatName(String catName) {
this.catName = catName;
}
}
MainActivity.java
private void fetchData(){
Retrofit retrofit = RetrofitClient.getInstance();
ApiService myApi = retrofit.create(ApiService.class);
myApi.getData().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<Model>>() {
@Override
public void onSubscribe(Disposable d) {
d.dispose();
}
@Override
public void onNext(List<Model> models) {
if(models.size() > 0){
progress.setVisibility(View.INVISIBLE);
adapter = new PostAdapter(getApplicationContext(),list);
recycler.setAdapter(adapter);
}
}
@Override
public void onError(Throwable e) {
Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete() {
}
});
}
PostAdapter.java
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
List<Model> list = new ArrayList<>();
Context context;
public PostAdapter(Context context,List<Model> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public PostAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_row,parent,false);
ViewHolder view = new ViewHolder(v);
return view;
}
@Override
public void onBindViewHolder(@NonNull PostAdapter.ViewHolder holder, int position) {
Model model = list.get(position);
holder.catName.setText(model.getCatName());
holder.catId.setText(model.getCatId());
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView catId,catName,comp,titl;
public ViewHolder(@NonNull View itemView) {
super(itemView);
catId = itemView.findViewById(R.id.catId);
catName = itemView.findViewById(R.id.catName);
}
}
}
Someone please let me know what I am doing wrong any help would be appreciated.
THANKS
Upvotes: 1
Views: 711
Reputation: 37404
You are calling d.dispose();
in onSubscribe
which will dispose the resource and result so remove dispose call as
public void onSubscribe(Disposable d) {
//d.dispose(); remove it
}
you can move dispose
in onDestroy
to free up resources(JIC request is still running) when activity(or fragment) is going to be removed from memory and make sure you have layout manager set on recycler view.
Upvotes: 1