Код: Выделить всё
E/RecyclerView: No adapter attached; skipping layout
< /code>
Форма ответа Сервер: < /p>
[
{
"term_id": "4",
"name": "Entertainment"
},
{
"term_id": "5",
"name": "Tech & Gadgets"
},
{
"term_id": "6",
"name": "Sports"
},
{
"term_id": "7",
"name": "Health and Fitness Tips"
}
]
sstrong> 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;
}
}
Код: Выделить всё
public interface ApiService {
@GET("Categoery_api")
Observable getData();
}
Код: Выделить всё
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;
}
}
Код: Выделить всё
private void fetchData(){
Retrofit retrofit = RetrofitClient.getInstance();
ApiService myApi = retrofit.create(ApiService.class);
myApi.getData().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
d.dispose();
}
@Override
public void onNext(List 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() {
}
});
}
Код: Выделить всё
public class PostAdapter extends RecyclerView.Adapter
{
List list = new ArrayList();
Context context;
public PostAdapter(Context context,List 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);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/603 ... rom-an-api
Мобильная версия