Ответ, который вы используете, неправильный, вам нужно исправить его следующим образом
Пример ответа json
{
"category": [
{
"categoryID": 5,
"categoryName": "Name",
"categoryImage": "path",
"categoryProductCount": 0,
"hasSubCategory": false
},
{
"categoryID": 5,
"categoryName": "Name",
"categoryImage": "path",
"categoryProductCount": 0,
"hasSubCategory": false
}
]
}
Теперь класс POJO, который вы должны использовать в интерфейсе
public class MyPojo
{
private List<Category> category;
public List<Category> getCategory ()
{
return category;
}
public void setCategory (List<Category> category)
{
this.category = category;
}
}
, где Category
класс равен
public class Category
{
private String categoryImage;
private String hasSubCategory;
private String categoryName;
private String categoryID;
private String categoryProductCount;
public String getCategoryImage ()
{
return categoryImage;
}
public void setCategoryImage (String categoryImage)
{
this.categoryImage = categoryImage;
}
public String getHasSubCategory ()
{
return hasSubCategory;
}
public void setHasSubCategory (String hasSubCategory)
{
this.hasSubCategory = hasSubCategory;
}
public String getCategoryName ()
{
return categoryName;
}
public void setCategoryName (String categoryName)
{
this.categoryName = categoryName;
}
public String getCategoryID ()
{
return categoryID;
}
public void setCategoryID (String categoryID)
{
this.categoryID = categoryID;
}
public String getCategoryProductCount ()
{
return categoryProductCount;
}
public void setCategoryProductCount (String categoryProductCount)
{
this.categoryProductCount = categoryProductCount;
}
}
**Usage**
public interface ApiInterface {
@GET("/categories/0")
Call<MyPojo> getCategoryList();
}
во фрагменте CategoriesFragment class
public class CategoriesFragment extends Fragment {
RecyclerView mRecyclerView;
List<Category> categoryList;
Category category;
public CategoriesFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_categories, container, false);
mRecyclerView = view.findViewById(R.id.recyclerview);
GridLayoutManager mGridLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecyclerView.setLayoutManager(mGridLayoutManager);
ApiInterface apiInterface = ApiClient.getRetrofitInstance().create(ApiInterface.class);
Call<MyPojo> call = apiInterface.getCategoryList();
call.enqueue(new Callback<MyPojo>() {
@Override
public void onResponse(Call<MyPojo> call, Response<MyPojo> response) {
if (response.isSuccessful()) {
categoryList = response.body().getCategory ();
CategoryAdapter myAdapter = new CategoryAdapter(getActivity(), categoryList);
mRecyclerView.setAdapter(new CategoryAdapter(category, R.layout.category_item_view, ));
// mRecyclerView.setAdapter(myAdapter);
}
else
// ApiErrorUtils.parseError(response);
Log.d("Api hata", "");
}
@Override
public void onFailure(Call<MyPojo> call, Throwable t) {
Log.d("Error", t.getMessage());
}
});
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
// throw new RuntimeException(context.toString()
// + " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}