开发者社区> 问答> 正文

我如何在Android Studio的Json解析器中放置一些条件

这是我的json URL https://jsonplaceholder.typicode.com/todos我只想显示已完成:确实是正确的,我该怎么做?

MainActivity.java

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("https://jsonplaceholder.typicode.com/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();

JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);

Call<List<Post>> call = jsonPlaceHolderApi.getPosts();

call.enqueue(new Callback<List<Post>>() {
    @Override
    public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
        if (!response.isSuccessful()) {
            textresult.setText("Code: " + response.code());
            return;
        }
        List<Post> posts = response.body();
        for (Post post : posts) {
            String content = "";
            content += "User ID: " + post.getUserId() + "\n";
            content += "ID: " + post.getId() + "\n";
            content += " Title: " + post.getTitle() + "\n";
            content += "Completed: " + post.getCompleted() + "\n\n";

            textresult.append(content);

            if (post.getCompleted().contains("true")) {
                textresult.setPaintFlags(textresult.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
            }
        }
    }
    @Override
    public void onFailure(Call<List<Post>> call, Throwable t) {
        textresult.setText(t.getMessage());
    }
});

JsonPlaceHolderApi.java

public interface JsonPlaceHolderApi {

    @GET("todos")                

    Call<List<Post>> getPosts();       


}

Post.java

public class Post {

    private int userId;

    private int id;

    private String title;

    private String completed;

    public int getUserId() {
        return userId;
    }

    public int getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getCompleted() {
        return completed;
    }
}

当我尝试运行上面的代码时,我得到的输出为image,但是如果完成,则需要:true,应该删除。

展开
收起
几许相思几点泪 2019-12-16 20:27:22 532 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载

相关镜像