Почему я не могу получить ответ с Retrofit2? - PullRequest
0 голосов
/ 30 сентября 2019

Я не пользовался дооснащением раньше. Но в этом проекте я буду использовать модернизацию. Когда я пытаюсь получить ответ от сервера, я не могу его получить. Это делает эту ошибку:

java.lang.IllegalStateException: ожидаемый BEGIN_OBJECT, но был BEGIN_ARRAY в строке 1 пути 2 столбца $

В чем здесь проблема?

Это мой почтальон: 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);
    }
}

Ответы [ 4 ]

1 голос
/ 30 сентября 2019

Это распространенная ошибка, которую я получаю, когда бэкэнд-команда превращает массив в объект.

Проблема в том, что где-то в вашем классе модели (класс pojo) вы были объявлены как массив, но на самом деле это был объект (или может быть наоборот).

1 голос
/ 30 сентября 2019

Ответ, который вы используете, неправильный, вам нужно исправить его следующим образом

Пример ответа 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);
    }
}
1 голос
/ 30 сентября 2019

Ваш 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());
            }
        });
0 голосов
/ 01 октября 2019

RetrofitHelper библиотека, написанная на kotlin, позволит вам делать вызовы API, используя несколько строк кода.

Добавьте заголовки в свой класс приложения следующим образом:

class Application : Application() {

    override fun onCreate() {
    super.onCreate()

        retrofitClient = RetrofitClient.instance
                    //api url
                .setBaseUrl("https://reqres.in/")
                    //you can set multiple urls
        //                .setUrl("example","http://ngrok.io/api/")
                    //set timeouts
                .setConnectionTimeout(4)
                .setReadingTimeout(15)
                    //enable cache
                .enableCaching(this)
                    //add Headers
                .addHeader("Content-Type", "application/json")
                .addHeader("client", "android")
                .addHeader("language", Locale.getDefault().language)
                .addHeader("os", android.os.Build.VERSION.RELEASE)
            }

        companion object {
        lateinit var retrofitClient: RetrofitClient

        }
    }  

И затем сделайте ваш звонок:

retrofitClient.Get<GetResponseModel>()
            //set path
            .setPath("api/users/2")
            //set url params Key-Value or HashMap
            .setUrlParams("KEY","Value")
            // you can add header here
            .addHeaders("key","value")
            .setResponseHandler(GetResponseModel::class.java,
                object : ResponseHandler<GetResponseModel>() {
                    override fun onSuccess(response: Response<GetResponseModel>) {
                        super.onSuccess(response)
                        //handle response
                    }
                }).run(this)

Для получения дополнительной информации см. Документацию

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...