android - 预期为 BEGIN_ARRAY 但在第 1 行第 2 列路径 $ - Retrofit 2 Android 中为 BEGIN_OBJECT

标签 android json retrofit retrofit2

我收到了这个改造2

ERROR: java.lang.illegalstateexception: expected begin_array but was begin_object

我不知道怎么解决

我在这里包含了我的完整代码

但我的错误是

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

怎么了?

这是我的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;
}}

API接口(interface)

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

最佳答案

你需要创建另一个类

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

关于android - 预期为 BEGIN_ARRAY 但在第 1 行第 2 列路径 $ - Retrofit 2 Android 中为 BEGIN_OBJECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55490926/

相关文章:

android - rxJava和改造多次调用的另一个请求中的另一个请求

android - 如何解决不幸的进程 "android.process.acore has stopped"错误?

java - INSERT 查询在 SQL 中有效,但在 Android 应用程序中无效

php - 使用 Eloquent 的 Laravel block 方法

java - 使用 Retrofit 的 Android 范围问题

java - Retrofit 表示注销时存在身份验证 token

php - Android PHP和mysql的最佳教程

Android RelativeLayout : How to put a view above another view, 不一定触摸?

java - Android 错误/点击崩溃

java - 如何通过浏览器/ postman 发送 GET 请求中的 json 数据?