错误:java.lang.IllegalStateException:应为begin_array,但为begin_object
我不知道怎么解决这个问题
我在这里包含了我的完整代码
这是我的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;
}}
public interface ApiInterface {
@GET("fom_combine.php")
Call<List<Search_Movie>> getContact(
@Query("name") String keyword
);}
....
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;
}
}
然后将调用
更改为
>调用=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();
}
});
在阅读@Ridcully的响应之后,我想问一下是否有一种方法可以更新,以便它知道JSON是一个数组。比如?
我正在学习youtube视频的改装,但现在我卡住了。它显示一个错误“reverfit expected begin_array but was begin_object at line 1 column 2 path$”我正在尝试从这个站点获取json数据。http://servicio-monkydevs.rhcloud.com/clientes/ ClientService.java clie
我使用django作为服务器端 在django中,我像这样返回Json 我不知道为什么会产生问题
我做错了什么? 当我这样读json时,它工作得很好
我怎么解决这个?
因此,在我的应用程序中,用户可以为他们的饭菜拍照,通过使用TensorFlow的图像分类,它将在中对饭菜进行分类。然后,使用Edamam食谱搜索API,它将向用户返回该餐的食谱。 这是我的中的中的内容: 对于、和,我在活动的顶部创建了以下变量: 更新1:我包含了使用后的JSON响应。 更新2:我已经包含了Recipe类。 有人知道如何解决这个问题吗?