dongyimo1293 2019-04-03 08:50
浏览 151
已采纳

预计BEGIN_ARRAY但在第1行第2栏路径为BEGIN_OBJECT $ - Retrofit 2 Android

I have received this retrofit 2

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

I don't know how to fix this

I have included my full code here

but My error is

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

What was wrong?

this is my 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
"+t.toString(), Toast.LENGTH_LONG).show();
        }
    });
}
  • 写回答

2条回答 默认 最新

  • dongyan7172 2019-04-03 08:58
    关注

    You need to create another class

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

    then change Call<List<Search_Movie>> call = apiInterface.getContact(key); to

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

    And the same with

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

    To

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

    And then

    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
    "+t.toString(), Toast.LENGTH_LONG).show();
                }
            }); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上