gpt4 book ai didi

java - 为什么使用 Retrofit2 无法得到响应?

转载 作者:行者123 更新时间:2023-12-01 19:35:00 26 4
gpt4 key购买 nike

我之前没怎么用过retrofit。但在这个项目中我将使用改造。当尝试从服务器获取响应时,无法得到响应。它会出现此错误:

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $

这里有什么问题吗?

这是我的 postman 结果:

enter image description here

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<Category>而不是<Category> 。就像下面这样

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());
}
});

关于java - 为什么使用 Retrofit2 无法得到响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58166894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com