我以前不怎么使用改造。但在这个项目中,我将使用改造。当尝试从服务器获取响应时,无法获得响应。它犯了这个错误:
java. lang.IllegalStateException:预期BEGIN_OBJECT,但BEGIN_ARRAY在第1行第2列路径$
这里有什么问题?
这是我的邮递员结果:
ApiInterface.java
public interface ApiInterface {
@GET("/categories/0")
Call<Category> getCategoryList();
}
ApiClient.java
public class ApiClient {
private static Retrofit retrofit;
private static final String BASE_URL = MyConstants.URL;
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
CategoryFragment.java
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<Category> call = apiInterface.getCategoryList();
call.enqueue(new Callback<Category>() {
@Override
public void onResponse(Call<Category> call, Response<Category> response) {
if (response.isSuccessful()) {
Category category_list = response.body();
Log.d("cateogry", "");
// 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<Category> 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);
}
}
您的API
响应需要Object
,但实际响应是Array
。您应该使用List
public interface ApiInterface {
@GET("/categories/0")
Call<List<Category>> getCategoryList();
}
API呼叫应该像跟随。
Call<List<Category>> call = apiInterface.getCategoryList();
call.enqueue(new Callback<List<Category>>() {
@Override
public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {
if (response.isSuccessful()) {
List<Category> category_list = response.body();
Log.d("cateogry",category_list.size());
// 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<List<Category>> call, Throwable t) {
Log.d("Error", t.getMessage());
}
});
这是后端团队将数组更改为对象时遇到的常见错误。
问题是在您的模型类(pojo类)中的某个地方,您被声明为一个数组,但实际上它是对象(反之亦然)。
你使用的响应不正确,需要这样纠正
示例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;
}
}
其中类别
类是
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();
}
在片段类别片段类
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);
}
}