Ожидаемый BEGIN_ARRAY, но был BEGIN_OBJECT в строке 1, путь 2 столбца $ - дооснащение 2 Android - PullRequest
2 голосов
/ 03 апреля 2019

Я получил эту модификацию 2

ОШИБКА: java.lang.illegalstateexception: ожидаемый begin_array, но был begin_object

Я не знаю, как это исправить

Я включил свой полный код здесь

но моя ошибка

E / ddd: onFailure: java.lang.IllegalStateException: ожидается BEGIN_ARRAY, но был BEGIN_OBJECT в строке 1, столбец 2, путь $

Что не так?

это мой JSON

 {"fom_combine":[{"mTitle":"Talaash","mYear":"2012"},{"mTitle":"Race 2","mYear":"2013"},{"mTitle":"October","mYear":"2018"},{"mTitle":"MS Dhoni: The Untold Story","mYear":"2016"},{"mTitle":"Phantom","mYear":"2015"},{"mTitle":"Baby","mYear":"2015"}]}

Search_Movie.class

public class Search_Movie {
@SerializedName("mTitle")
@Expose
private String mTitle;

@SerializedName("mYear")
@Expose
private long mYear;

public Search_Movie(String mTitle, long mYear) {
    this.mTitle = mTitle;
    this.mYear = mYear;
}

public String getmTitle() {
    return mTitle;
}

public long getmYear() {
    return mYear;
}}

ApiClient.java

public class ApiClient {
public static final String BASE_URL = "https://example.com/search/";
public static Retrofit retrofit;

public static Retrofit getApiClient(){
    if (retrofit==null){
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}}

ApiInterface

public interface ApiInterface {
@GET("fom_combine.php")
Call<List<Search_Movie>> getContact(
        @Query("name") String keyword
);}

Search_Activity.java

....
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private List<Search_Movie> contacts;
private Search_Adapter adapter;
private ApiInterface apiInterface;
ProgressBar progressBar;

    ....

    progressBar = findViewById(R.id.prograss);
    recyclerView = findViewById(R.id.recyclerView);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    fetchContact( "");

   .....
       public void fetchContact( String key){

    apiInterface = ApiClient.getApiClient().create(ApiInterface.class);

    Call<List<Search_Movie>> call = apiInterface.getContact(key);
    call.enqueue(new Callback<List<Search_Movie>>() {
        @Override
        public void onResponse(Call<List<Search_Movie>> call, Response<List<Search_Movie>> response) {
            progressBar.setVisibility(View.GONE);
            contacts = response.body();
            adapter = new Search_Adapter(contacts, Search_Activity.this);
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(Call<List<Search_Movie>> call, Throwable t) {
            progressBar.setVisibility(View.GONE);
            Toast.makeText(Search_Activity.this, "Error\n"+t.toString(), Toast.LENGTH_LONG).show();
        }
    });
}

Ответы [ 2 ]

3 голосов
/ 03 апреля 2019

Вам нужно создать еще один класс

public class FomCombine {
    @SerializedName("fom_combine")
    @Expose
    private List<Search_Movie> fom_combine;



    public FomCombine(List<Search_Movie> fom_combine) {this.fom_combine = fom_combine;}

public List<Search_Movie> getfom_combine() {
    return fom_combine;
}

 }

затем поменяй Call<List<Search_Movie>> call = apiInterface.getContact(key); до

Call<FomCombine> call = apiInterface.getContact(key);

И то же самое с

@GET("fom_combine.php")
Call<List<Search_Movie>> getContact(
        @Query("name") String keyword
);}

К

@GET("fom_combine.php")
    Call<FomCombine> getContact(
            @Query("name") String keyword
    );}

А потом

Call<FomCombine> call = apiInterface.getContact(key);
        call.enqueue(new Callback<FomCombine>() {
            @Override
            public void onResponse(Call<FomCombine> call, Response<FomCombine> response) {
                progressBar.setVisibility(View.GONE);
                contacts = response.body().getfom_combine();
                adapter = new Search_Adapter(contacts, Search_Activity.this);
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<FomCombine> call, Throwable t) {
                progressBar.setVisibility(View.GONE);
                Toast.makeText(Search_Activity.this, "Error\n"+t.toString(), Toast.LENGTH_LONG).show();
            }
        }); 
0 голосов
/ 03 апреля 2019

json, который вы предоставляете, является объектом, а не массивом.Вы можете указать только часть с []

[
    {"mTitle":"Talaash","mYear":"2012"},
    {"mTitle":"Race 2","mYear":"2013"},
    {"mTitle":"October","mYear":"2018"},
    {"mTitle":"MS Dhoni: The Untold Story","mYear":"2016"},
    {"mTitle":"Phantom","mYear":"2015"},
    {"mTitle":"Baby","mYear":"2015"}
]

, а не с {fom_combine : part

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