如何在没有 api 键的情况下使用改造调用 REST api 并将其显示在 Android 的列表视图中?

How to call a REST api using retrofit without api key and present it in listview in Android?

REST API 是“http://citywall.in/category.php

O/P 代码:

{
    "category": [{
        "id": "1",
        "name": "Recipe",
        "image": "receipe.jpg",
        "createdDate": "2019-03-27 00:00:00"
    }, {
        "id": "2",
        "name": "Gardening",
        "image": "gardening.jpg",
        "createdDate": "2019-03-27 00:00:00"
    }, {
        "id": "3",
        "name": "Services",
        "image": "services.jpg",
        "createdDate": "2019-03-27 00:00:00"
    }, {
        "id": "4",
        "name": "Tourism",
        "image": "tourism.jpg",
        "createdDate": "2019-03-27 00:00:00"
    }, {
        "id": "5",
        "name": "Lifestyle",
        "image": "lifestyle.jpg",
        "createdDate": "2019-03-27 00:00:00"
    }, {
        "id": "6",
        "name": "Other",
        "image": "other.jpg",
        "createdDate": "2019-03-27 00:00:00"
    }]
}

首先需要实例化Retrofit:

   Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://citywall.in/")
    .build();

然后你应该为类别列表中的每个项目创建和接口,其中 Category 对应 Class:

public interface CategoryServiceInterface{
    @Get("/category.php")
    Call<Response> getCategoryList(); 
}

Response class是一个数据 class:

   public class Response{
        @SerializedName("category")
        private List<Category> categoryList = new ArrayList()

        /**
            Getters and setters....
        */
   }

   public class Category{
       @SerializedName("id")
       private String id;
       @SerializedName("name")
       private String name;
       @SerializedName("image")
       private String imageName;
       @SerializedName("createdDate")
       private Date createdDate;

       /**
            Getters and setters....
        */
   }

现在你已经掌握了所有基础知识。只需拨打以下电话:

   CategoryServiceInterface service = retrofit.create(CategoryServiceInterface.class)

   Response response = service.getCategoryList().body()

现在您的数据模型需要出现在 android 列表中。更多详细信息,请参阅以下 link: https://square.github.io/retrofit/