[英]How to call a REST api using retrofit without api key and present it in listview in Android?
REST API is " http://citywall.in/category.php " REST API 是“ http://citywall.in/category.php ”
O/P of code : 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"
}]
}
Fist you need to instantiate Retrofit:拳头你需要实例化改造:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://citywall.in/")
.build();
Then you should create and interface where Category
is corresponding Class for each item in category list:然后你应该创建和接口,其中
Category
是类别列表中每个项目对应的 Class:
public interface CategoryServiceInterface{
@Get("/category.php")
Call<Response> getCategoryList();
}
Response
class is a data class: Response
类是一个数据类:
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....
*/
}
Now you have all fundamental.现在你有了所有的基础。 just call following:
只需拨打以下电话:
CategoryServiceInterface service = retrofit.create(CategoryServiceInterface.class)
Response response = service.getCategoryList().body()
Now you have data model needed to be present in android list.现在您需要在 android 列表中出现数据模型。 For more details refer to following link.: https://square.github.io/retrofit/
有关更多详细信息,请参阅以下链接。: https : //square.github.io/retrofit/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.