public class LatestNews
{
@SerializedName("duration")
public String duration;
@SerializedName("data")
public List<LNews> lnews = new ArrayList<>();
public class LNews
{
@SerializedName("id")
public String id;
@SerializedName("title")
public String title;
@SerializedName("description")
public String description;
@SerializedName("date_of_publish")
public String date_of_publish;
@SerializedName("category")
public String category;
@SerializedName("subcategory")
public String subcategory;
@SerializedName("image")
public String image;
@SerializedName("video")
public String video;
}
}
用于连接的改造客户端
public class APIClient
{
public static Retrofit retrofit = null;
public static Retrofit getClient()
{
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder()
.baseUrl(ConnectionURLs.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
}
API接口从数据库获取响应
public interface APIInterface
{
@GET("get_all_latest_news/")
Call<ResponseHome> getLatestNews();
}
用于翻新API的POJO类
public class ResponseHome
{
@SerializedName("latest_news")
public List<LatestNews> latestnews = new ArrayList<>();
}
在MainActivity.java
apiInterface = APIClient.getClient().create(APIInterface.class);
Call<ResponseHome> call = apiInterface.getLatestNews();
call.enqueue(new Callback<ResponseHome>()
{
@Override
public void onResponse(Call<ResponseHome> call, Response<ResponseHome> response)
{
String displayResponse = "";
ResponseHome resource = response.body();
List<LatestNews> datumList = resource.latestnews;
ArrayList<LatestNews> latestNews=new ArrayList<>();
for (int i=0;i<datumList.size();i++)
{
//latestNews.add(datumList.);
displayResponse += datumList.get(i).duration +"\n";
//LatestNews.LNews newsList= (LatestNews.LNews) datumList.get(i).lnews;
for (LatestNews.LNews newsList:datumList.get(i).lnews)
{
displayResponse += newsList.title+"\n"+newsList.description ;
Log.d(TAG, "$$$$$$$$$$$$$$$$$$$$");
}
}
Log.d(TAG, "onResponse: "+displayResponse);
}
@Override
public void onFailure(Call<ResponseHome> call, Throwable t)
{
//call.cancel();
Log.d(TAG, "onFailure: 666666666666666666666666666666\n"+t.getMessage());
}
});
异常:D / HomeFragment:onFailure:666666666666666666666666666666 java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在行1的第50列$ .latest_news [0] .data
处为BEGIN_OBJECT