![](/img/trans.png)
[英]Retrofit2 with Android Studio: Cant get the array of petrol-stations
[英]Retrofit cant get json(android)
對不起我的英語不好。 我嘗試為android學習改造(在我使用凌空之前)。 我不知道為什么它不起作用。
我的班級對象帖子
private String userId;
private String id;
private String title;
private String body;
//the getters and setters
界面鏈接
public interface Links {
@GET("/posts")
Call<PostsS> getPosts();
}
和我的主要
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://jsonplaceholder.typicode.com")
.build();
Links service = retrofit.create(Links.class);
Call<PostsS> call = service.getPosts();
call.enqueue(new Callback<PostsS>() {
@Override
public void onResponse(retrofit2.Response<PostsS> response) {
Log.e("responce", response.toString());
}
@Override
public void onFailure(Throwable t) {
Log.e("error", t.toString());
}
});
我有
Caused by: java.lang.IllegalArgumentException: Unable to create converter for class com.example.alexy.myapplication.PostsS
for method Links.getPosts
我的json
[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
{
"userId": 1,
"id": 2,
"title": "qui est esse",
"body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
}
]
通過改造2,您必須明確指定要使用的轉換器。 例如
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://jsonplaceholder.typicode.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
你會Gson作為轉換器。 您還必須在build.gradle
文件中添加build.gradle
。 例如
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
嘗試使用相同的-beta2
來解決所有與-beta2
相關的問題。
編輯
根據您發布的JSON,您的方法應該返回
Call<List<PostsS>>
而不是Call<PostsS>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.