【发布时间】:2020-10-07 11:16:34
【问题描述】:
我正在使用 Retrofit 使用 java 创建简单的 Android 项目。 但是改造返回null。
我的JSON
[
{
"id": "1536",
"id_category": "7"
},
{
"id": "1550",
"id_category": "7"
}
]
Place.java
public class Place {
@SerializedName("id")
private int id;
@SerializedName("id_category")
private int id_category;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public int getId_category() { return id_category; }
public void setId_category(int id_kategori) { this.id_category = id_kategori; }
}
PlaceService.java
public interface PlaceService {
@FormUrlEncoded
@POST("places/get_location_by_id_category")
Call<List<Place>> getPlaceByIdCategory(
@Field("id_category") int id_category
);
}
ApiHttpClient.java
public class ApiHttpClient {
private static Retrofit retrofit = null;
public static Retrofit getRetrofit(){
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl("http://admin.tapaleuk.id/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
public static PlaceService getPlaceService(){
PlaceService placeService = getRetrofit().create(PlaceService.class);
return placeService;
}
}
还有Activity中的代码 私人无效read_id_by_category(int id_category){ progressBar.setVisibility(View.VISIBLE);
Activity activity = getActivity();
if (activity != null) {
Call<List<Place>> locationList = ApiHttpClient.getPlaceService().getPlaceByIdCategory(id_category);
locationList.enqueue(new Callback<List<Place>>() {
@Override
public void onResponse(Call<List<Place>> call, Response<List<Place>> response) {
if (response.isSuccessful()) {
location_list = response.body();
total_location = location_list.size();
id_location_array = new int[total_location];
id_category_array = new int[total_location];
for (int i = 0; i < total_location; i++) {
id_location_array[i] = location_list.get(i).getId();
id_category_array[i] = location_list.get(i).getId_category();
}
}
}
@Override
public void onFailure(Call<List<Place>> call, Throwable t) {
t.printStackTrace();
}
});
}
progressBar.setVisibility(View.GONE);
}
代码“total_location = location_list.size();”中的total_location为空
谢谢
【问题讨论】:
-
在模型类中将 int 更改为字符串并检查。