【问题标题】:how to use retrofit with wordpress json api如何使用 wordpress json api 进行改造
【发布时间】:2016-09-06 10:49:41
【问题描述】:

我尝试使用简单的 json 响应进行改造,该响应具有 [ {},{},{},{},{},{},] 对象数组,但当我尝试使用状态进行改造时:

{"ok",count: 4,count_total: 4,pages: 1,posts: [{},{},{},{}] }

我想出了空结果..请找到正确的解决方案我如何调用改造以获得正确的结果。

pojo 类`

public class Categories {
   @SerializedName("status")
  private int status;
    @SerializedName("id")
   private int id;
  @SerializedName("type")
  private String type;
 @SerializedName("title")
  private  String title;
  @SerializedName("content")
  private String content;

 public int getId() {
    return id;
}

 public void setId(int id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}
}

ma​​inactivity.java

    public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    ApiInterface apiService =
            ApiClient.getClient().create(ApiInterface.class);

    Call<List<Categories>> call = apiService.response();
    call.enqueue(new Callback<List<Categories>>() {
        @Override
        public void onResponse(Call<List<Categories>> call, Response<List<Categories>> response) {

            List<Categories> movies = response.body();
            recyclerView
                    .setAdapter(new MoviesAdapter(movies,
                            R.layout.list_item_movie, getApplicationContext()));

        }

        @Override
        public void onFailure(Call<List<Categories>> call, Throwable t) {

        }
    });

}

apiclient.java

public class ApiClient {

  public static final String BASE_URL = "http://androidaura.com/health/api/get_recent_posts/";
  private static Retrofit retrofit = null;


  public static Retrofit getClient() {
     if (retrofit==null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}
}

【问题讨论】:

    标签: android json rest retrofit2


    【解决方案1】:

    您应该将您的POJO 更改为以下内容:

    Public class Model {
    
          String status; // because "ok" is string not boolean
          int count;
          int count_total;
          int pages;
          List<Post> posts;
    
    
          public class Post {
    
            // add attribute of your old pojo 
    
          }
    }
    

    还要确保您的类属性与服务器响应相同;

    【讨论】:

    • 我已经这样做了,但仍然出现 nulll 引用错误我已经创建了一个单独的类用于响应,其中包含帖子列表并从中调用 getposts 方法
    • @rahul printStackTrace 在您的 onFailure 方法中查看什么是错误。
    • java.lang.NullPointerException:尝试在 info.retrofit.activity.MainActivity 的空对象引用上调用虚拟方法“java.util.List info.retrofit.model.PostResponse.getPostsList()” $1.onResponse
    • public void setPostsList(List postsList) { this.postsList = postsList; }
    • 你确定服务器返回 List 吗?
    猜你喜欢
    • 2016-12-31
    • 2019-01-17
    • 2018-01-18
    • 2018-08-25
    • 1970-01-01
    • 2017-01-11
    • 2017-08-05
    • 2018-09-08
    • 1970-01-01
    相关资源
    最近更新 更多