某些值返回null。 BetterFeaturedImage betterFeaturedImage null返回。在主要的Pojo类之外,还有31个。虽然我做了可序列化的类,但有些值返回null。我应该如何进行? gradle也没有问题
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:retrofit-converters:2.4.0'
POJO CLASS
public class GundemHaberleri implements Serializable {
private String date;
private String template;
private Links links;
private String link;
private String type;
private Title title;
private Content content;
private int featuredMedia;
private String modified;
private int id;
private List<Integer> categories;
private String dateGmt;
private String slug;
private String modifiedGmt;
private int author;
private String format;
private String commentStatus;
private List<Object> tags;
private String pingStatus;
private List<Object> meta;
private boolean sticky;
private Guid guid;
private BetterFeaturedImage betterFeaturedImage;
private Excerpt excerpt;
private String status;
public void setDate(String date){
this.date = date;
}
public String getDate(){
return date;
}
public void setTemplate(String template){
this.template = template;
}
public String getTemplate(){
return template;
}
public void setLinks(Links links){
this.links = links;
}
public Links getLinks(){
return links;
}
public void setLink(String link){
this.link = link;
}
public String getLink(){
return link;
}
public void setType(String type){
this.type = type;
}
public String getType(){
return type;
}
public void setTitle(Title title){
this.title = title;
}
public Title getTitle(){
return title;
}
public void setContent(Content content){
this.content = content;
}
public Content getContent(){
return content;
}
public void setFeaturedMedia(int featuredMedia){
this.featuredMedia = featuredMedia;
}
public int getFeaturedMedia(){
return featuredMedia;
}
public void setModified(String modified){
this.modified = modified;
}
public String getModified(){
return modified;
}
public void setId(int id){
this.id = id;
}
public int getId(){
return id;
}
public void setCategories(List<Integer> categories){
this.categories = categories;
}
public List<Integer> getCategories(){
return categories;
}
public void setDateGmt(String dateGmt){
this.dateGmt = dateGmt;
}
public String getDateGmt(){
return dateGmt;
}
public void setSlug(String slug){
this.slug = slug;
}
public String getSlug(){
return slug;
}
public void setModifiedGmt(String modifiedGmt){
this.modifiedGmt = modifiedGmt;
}
public String getModifiedGmt(){
return modifiedGmt;
}
public void setAuthor(int author){
this.author = author;
}
public int getAuthor(){
return author;
}
public void setFormat(String format){
this.format = format;
}
public String getFormat(){
return format;
}
public void setCommentStatus(String commentStatus){
this.commentStatus = commentStatus;
}
public String getCommentStatus(){
return commentStatus;
}
public void setTags(List<Object> tags){
this.tags = tags;
}
public List<Object> getTags(){
return tags;
}
public void setPingStatus(String pingStatus){
this.pingStatus = pingStatus;
}
public String getPingStatus(){
return pingStatus;
}
public void setMeta(List<Object> meta){
this.meta = meta;
}
public List<Object> getMeta(){
return meta;
}
public void setSticky(boolean sticky){
this.sticky = sticky;
}
public boolean isSticky(){
return sticky;
}
public void setGuid(Guid guid){
this.guid = guid;
}
public Guid getGuid(){
return guid;
}
public void setBetterFeaturedImage(BetterFeaturedImage betterFeaturedImage){
this.betterFeaturedImage = betterFeaturedImage;
}
public BetterFeaturedImage getBetterFeaturedImage(){
return betterFeaturedImage;
}
public void setExcerpt(Excerpt excerpt){
this.excerpt = excerpt;
}
public Excerpt getExcerpt(){
return excerpt;
}
public void setStatus(String status){
this.status = status;
}
public String getStatus(){
return status;
}
@Override
public String toString(){
return
"GundemHaberleri{" +
"date = '" + date + '\'' +
",template = '" + template + '\'' +
",_links = '" + links + '\'' +
",link = '" + link + '\'' +
",type = '" + type + '\'' +
",title = '" + title + '\'' +
",content = '" + content + '\'' +
",featured_media = '" + featuredMedia + '\'' +
",modified = '" + modified + '\'' +
",id = '" + id + '\'' +
",categories = '" + categories + '\'' +
",date_gmt = '" + dateGmt + '\'' +
",slug = '" + slug + '\'' +
",modified_gmt = '" + modifiedGmt + '\'' +
",author = '" + author + '\'' +
",format = '" + format + '\'' +
",comment_status = '" + commentStatus + '\'' +
",tags = '" + tags + '\'' +
",ping_status = '" + pingStatus + '\'' +
",meta = '" + meta + '\'' +
",sticky = '" + sticky + '\'' +
",guid = '" + guid + '\'' +
",better_featured_image = '" + betterFeaturedImage + '\'' +
",excerpt = '" + excerpt + '\'' +
",status = '" + status + '\'' +
"}";
}
JSON
{
"id": 3542,
"date": "2018-08-17T10:54:57",
"date_gmt": "2018-08-17T10:54:57",
"guid": {
"rendered": "https://www.gundemhaberleri.org/?p=3542"
},
"modified": "2018-08-17T10:54:57",
"modified_gmt": "2018-08-17T10:54:57",
"slug": "emre-akbabanin-mi-yoksa-alanyasporun-mu-istedigi-olacak",
"status": "publish",
"type": "post",
"link": "https://www.gundemhaberleri.org/emre-akbabanin-mi-yoksa-alanyasporun-mu-istedigi-olacak/",
"title": {
"rendered": "Emre Akbaba’nın mı Yoksa Alanyaspor’un mu İstediği Olacak"
},
"content": {
"rendered": "<p>Transfer döneminde en değerli yerli oyuncu olarak göze çarpan Alanyaspor’lu Emre Akbaba ile ilgili çelişkili haberler gelmeyte devam ederken; Oyuncunun hangi klübe transfer olacağı ise merak konusu. Dün İstanbulda önce Türk Telekom Arena stadında Galatasaray yöneticileri ile pazarlık yapan Alanyaspor Başkanı, ardından Fenerbahçeli yönetilerle bir araya gelerek Emre konusunda anlaştı.</p>\n<p>Zira Hasan Çavuşoğlu koyu bir Fenerbahçeli ve aynı zamanda da Alanya Fenerbahçeliler derneğinin de başkanı. Ancak Emre Akbaba’nın ısrarla Galatasaray’ı istemesi Başkanı kara kara düşündürüyor.</p>\n<p>Dün akşam Emre Akbaba takım arkadaşları ve teknik heyetle vedalaşarak takımının kampından ayrıldı. Bugün gün içinde İstanbula gitmesi ve yeni takımını belirleyecek olan sözleşmeyi imzalaması bekleniyor.</p>\n<p>Bu transferde Alanyaspor Başkanının isteğimi yoksa futbolcunun isteğimi gerçekleşecek merakla beklenen ise bu. Gelen bilgilere göre Alanyaspor başkanının taviz vermediği ve başkanın dediğinin olacağı bilgisi var. Zira geçtiğimiz yıllarda da Mehmet Topuz Beşiktaşlı olduğunu açıklamış ve Beşiktaş ile anlaşmış ancak Fenerbahçe oyuncunun klübüyle anlaşarak Mehmet Topuzu zoraki transfer etmişti.</p>\n<p>Taraftarlardanda bu transfere atıfta bulunarak zorla gerçekleşen transferlerin fayda getirmediği düşüncesi dile getiriliyor.</p>\n<p> </p>\n",
"protected": false
},
"excerpt": {
"rendered": "<p>Transfer döneminde en değerli yerli oyuncu olarak göze çarpan Alanyaspor’lu Emre Akbaba ile ilgili çelişkili haberler gelmeyte devam ederken; Oyuncunun hangi klübe transfer olacağı ise merak konusu. Dün İstanbulda önce Türk Telekom Arena stadında Galatasaray yöneticileri ile pazarlık yapan Alanyaspor Başkanı, ardından Fenerbahçeli yönetilerle bir araya gelerek Emre konusunda anlaştı. Zira Hasan Çavuşoğlu koyu bir […]</p>\n",
"protected": false
},
"author": 1,
"featured_media": 3543,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"template": "",
"format": "standard",
"meta": [],
"categories": [
6
],
"tags": [],
"better_featured_image": {
"id": 3543,
"alt_text": "",
"caption": "",
"description": "",
"media_type": "image",
"media_details": {
"width": 640,
"height": 893,
"file": "2018/08/emre-akbaba.jpg",
"sizes": {
"thumbnail": {
"file": "emre-akbaba-150x150.jpg",
"width": 150,
"height": 150,
"mime-type": "image/jpeg",
"source_url": "https://www.gundemhaberleri.org/wp-content/uploads/2018/08/emre-akbaba-150x150.jpg"
},
"medium": {
"file": "emre-akbaba-215x300.jpg",
"width": 215,
"height": 300,
"mime-type": "image/jpeg",
"source_url": "https://www.gundemhaberleri.org/wp-content/uploads/2018/08/emre-akbaba-215x300.jpg"
},
"publisher-tb1": {
"file": "emre-akbaba-86x64.jpg",
"width": 86,
"height": 64,
"mime-type": "image/jpeg",
"source_url": "https://www.gundemhaberleri.org/wp-content/uploads/2018/08/emre-akbaba-86x64.jpg"
},
"publisher-sm": {
"file": "emre-akbaba-210x136.jpg",
"width": 210,
"height": 136,
"mime-type": "image/jpeg",
"source_url": "https://www.gundemhaberleri.org/wp-content/uploads/2018/08/emre-akbaba-210x136.jpg"
},
"publisher-mg2": {
"file": "emre-akbaba-279x220.jpg",
"width": 279,
"height": 220,
"mime-type": "image/jpeg",
"source_url": "https://www.gundemhaberleri.org/wp-content/uploads/2018/08/emre-akbaba-279x220.jpg"
},
"publisher-md": {
"file": "emre-akbaba-357x210.jpg",
"width": 357,
"height": 210,
"mime-type": "image/jpeg",
"source_url": "https://www.gundemhaberleri.org/wp-content/uploads/2018/08/emre-akbaba-357x210.jpg"
},
"publisher-lg": {
"file": "emre-akbaba-640x430.jpg",
"width": 640,
"height": 430,
"mime-type": "image/jpeg",
"source_url": "https://www.gundemhaberleri.org/wp-content/uploads/2018/08/emre-akbaba-640x430.jpg"
},
"publisher-full": {
"file": "emre-akbaba-640x580.jpg",
"width": 640,
"height": 580,
"mime-type": "image/jpeg",
"source_url": "https://www.gundemhaberleri.org/wp-content/uploads/2018/08/emre-akbaba-640x580.jpg"
},
"publisher-tall-sm": {
"file": "emre-akbaba-180x217.jpg",
"width": 180,
"height": 217,
"mime-type": "image/jpeg",
"source_url": "https://www.gundemhaberleri.org/wp-content/uploads/2018/08/emre-akbaba-180x217.jpg"
},
"publisher-tall-lg": {
"file": "emre-akbaba-267x322.jpg",
"width": 267,
"height": 322,
"mime-type": "image/jpeg",
"source_url": "https://www.gundemhaberleri.org/wp-content/uploads/2018/08/emre-akbaba-267x322.jpg"
},
"publisher-tall-big": {
"file": "emre-akbaba-368x445.jpg",
"width": 368,
"height": 445,
"mime-type": "image/jpeg",
"source_url": "https://www.gundemhaberleri.org/wp-content/uploads/2018/08/emre-akbaba-368x445.jpg"
}
},
"image_meta": {
"aperture": "0",
"credit": "",
"camera": "",
"caption": "",
"created_timestamp": "0",
"copyright": "",
"focal_length": "0",
"iso": "0",
"shutter_speed": "0",
"title": "",
"orientation": "0",
"keywords": []
}
},
"post": 3542,
"source_url": "https://www.gundemhaberleri.org/wp-content/uploads/2018/08/emre-akbaba.jpg"
},
"_links": {
"self": [
{
"href": "https://www.gundemhaberleri.org/wp-json/wp/v2/posts/3542"
}
],
"collection": [
{
"href": "https://www.gundemhaberleri.org/wp-json/wp/v2/posts"
}
],
"about": [
{
"href": "https://www.gundemhaberleri.org/wp-json/wp/v2/types/post"
}
],
"author": [
{
"embeddable": true,
"href": "https://www.gundemhaberleri.org/wp-json/wp/v2/users/1"
}
],
"replies": [
{
"embeddable": true,
"href": "https://www.gundemhaberleri.org/wp-json/wp/v2/comments?post=3542"
}
],
"version-history": [
{
"count": 1,
"href": "https://www.gundemhaberleri.org/wp-json/wp/v2/posts/3542/revisions"
}
],
"predecessor-version": [
{
"id": 3544,
"href": "https://www.gundemhaberleri.org/wp-json/wp/v2/posts/3542/revisions/3544"
}
],
"wp:featuredmedia": [
{
"embeddable": true,
"href": "https://www.gundemhaberleri.org/wp-json/wp/v2/media/3543"
}
],
"wp:attachment": [
{
"href": "https://www.gundemhaberleri.org/wp-json/wp/v2/media?parent=3542"
}
],
"wp:term": [
{
"taxonomy": "category",
"embeddable": true,
"href": "https://www.gundemhaberleri.org/wp-json/wp/v2/categories?post=3542"
},
{
"taxonomy": "post_tag",
"embeddable": true,
"href": "https://www.gundemhaberleri.org/wp-json/wp/v2/tags?post=3542"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w.org/{rel}",
"templated": true
}
]
}
请求方法
private List<GundemHaberleri> list;
private RecyclerView recyclerView;
private HomeListAdapter homeListAdapter;
public void request(){
list = new ArrayList<>();
Call<List<GundemHaberleri>> gelenPosts = ManagerAll.getInstance().getirEkonomi();
gelenPosts.enqueue(new Callback<List<GundemHaberleri>>() {
@Override
public void onResponse(Call<List<GundemHaberleri>> call, Response<List<GundemHaberleri>> response) {
if (response.isSuccessful()){
list = response.body();
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
homeListAdapter = new HomeListAdapter(getApplicationContext(),list);
recyclerView.setAdapter(homeListAdapter);
}
}
@Override
public void onFailure(Call<List<GundemHaberleri>> call, Throwable t) {
Toast.makeText(getApplicationContext(),t.getLocalizedMessage(),Toast.LENGTH_LONG).show();
}
});
}
BaseManager
public class BaseManager {
//RestApi Back
protected RestApi getRestApiClient() {
RestApiClient restApiClient = new RestApiClient(BaseUrl.BASE_URL);
return restApiClient.getRestApi();
}
BaseUrl
public class BaseUrl {
public static final String BASE_URL = "http://www.gundemhaberleri.org";
ManagerAll
public class ManagerAll extends BaseManager {
private static ManagerAll ourgetInstance = new ManagerAll();
public static synchronized ManagerAll getInstance() {
return ourgetInstance;
}
public Call<List<GundemHaberleri>> getirPosts() {
Call<List<GundemHaberleri>> xx = getRestApiClient().getirPosts();
return xx;
}
RestApi
public interface RestApi {
@GET("/wp-json/wp/v2/posts")
Call<List<GundemHaberleri>> getirPosts();
}
RestApiClient
public class RestApiClient {
private RestApi mRestApi;
public RestApiClient(String restApiServiceUrl) {
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.connectTimeout(3, TimeUnit.SECONDS);
OkHttpClient okHttpClient = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(restApiServiceUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
mRestApi = retrofit.create(RestApi.class);
}
//RestApi Backkk
public RestApi getRestApi() {
return mRestApi;
}
}
答案 0 :(得分:0)
尝试使用http://www.jsonschema2pojo.org/生成POJO,因为我发现@SerializedName和@Expose未在您的类中列出。
答案 1 :(得分:0)
MyAdapterss
public class HomeListAdapter extends RecyclerView.Adapter<HomeListAdapter.HomeListViewHolder> {
Context context;
List<GundemHaberleri> list;
public HomeListAdapter(Context context, List<GundemHaberleri> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public HomeListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recycler_row,viewGroup,false);
return new HomeListViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull HomeListViewHolder homeListViewHolder, int i) {
GundemHaberleri gundemHaberleri = list.get(i);
homeListViewHolder.textViewTitle.setText(gundemHaberleri.getTitle().getRendered());
//Picasso.get().load(gundemHaberleri.getBetterFeaturedImage().getSourceUrl()).into(homeListViewHolder.imageView);
Log.i("gelen1" ,list.get(0).toString());
}
@Override
public int getItemCount() {
return list.size();
}
public class HomeListViewHolder extends RecyclerView.ViewHolder{
private CardView cardView;
private ImageView imageView;
private TextView textViewTitle;
public HomeListViewHolder(@NonNull View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.titleTextView);
imageView = itemView.findViewById(R.id.homeImageView);
cardView = itemView.findViewById(R.id.homeCardView);
}
}
}