Retrofit2 中的空对象
Null Object In Retrofit2
各位。
我正在尝试使用 retrofit2 连接到 RestApi,但这会给我错误或 return object null。
{"news": [ { "News": { "id": "1", "title": "Big data is changing the ace f fashion", "description": "Download Fashioning Data: A 2015 Update" } }]}
我有下一个代码:
界面休息
@GET("news")
Call<news> getNews();
POJO 对象:
public class news {
@SerializedName("News")
public List<Listnews> listnews;
public static class Listnews{
public News news;
public static class News{
@SerializedName("id")
private String id;
@SerializedName("title")
private String title;
@SerializedName("description")
private String description;
// getters and setters
}
}
并显示在mainActivity
Call<news> call2 = api.getNews();
call2.enqueue(new Callback<news>() {
@Override
public void onResponse(Call<news> call, Response<news> response) {
if (response.code() == 200){
List<news.Listnews> respuesta = response.body().listnews;
System.out.println( "title: "+ respuesta.get(0).news);
}
}
@Override
public void onFailure( Call<news> call, Throwable t ) {
System.out.println( t + " Error " );
}
});
但这曾经return无效。
您的 json 字符串中有两个 "news"、"News"。
由于您的模型使用以下注释
News(数组)
-> id
-> 标题
-> 描述
{
"News": [
{ "id": "1", "title": "Big data is changing the ace f fashion", "description": "Download Fashioning Data: A 2015 Update" },
{ "id": "2", "title": "Big data is changing the ace f fashion", "description": "Download Fashioning Data: A 2015 Update" }
]
}
但是在你的 json 字符串中
{
"news": [{
"News": { "id": "1", "title": "Big data is changing the ace f fashion", "description": "Download Fashioning Data: A 2015 Update" }
}]
}
可以看到结构:
新闻(数组)
->新闻(objects)
->id
->标题
->描述
因此,您无法在其中调整任何数据。
以上是你得到null的原因object,你应该使用第一个json结构来获取新闻数据
以下是对您工作的建议
不要混淆单词"news"和数据结构"News"(http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api) It is okay for you to use "news" as the path to get the list of the news. But it will be painful if you have another "News" or "Listnews" in the return json since it is quite confused to findout which one should be the news. Try another word like "data", "items" or just return an arraylist of the news since your path (http://restful.com/news)通知你正在尝试获取新闻列表
为您的 restful 构建测试用例。如果您尝试测试移动应用程序中的每个 restful api,那将会很痛苦。尝试使用以下
一个。为您构建工具测试 (https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html) 以演示按钮单击以触发 restful
乙。使用 retrofit(https://github.com/square/okhttp/tree/master/mockwebserver) 模拟请求
并将您的 json 字符串复制到转义字符中(使用此网站 http://bernhardhaeussner.de/odd/json-escape/)
这两种方式会节省你大量的开发时间
请在您的 class 中进行以下更改,希望这会奏效。如果没有,请告诉我。
public class news {
// Change 1 ---
@SerializedName("news")
public List<Listnews> listnews;
public static class Listnews{
// Change 2 ---
@SerializedName("News")
public News news;
public static class News{
@SerializedName("id")
private String id;
@SerializedName("title")
private String title;
@SerializedName("description")
private String description;
// getters and setters
} }
这对我有用。我不知道你为什么采取静态 class。我是这样简单地完成的:
POJO class News.java:
public class News
{
String id, title, description;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
}
API接口:
public interface NewsAPI
{
@POST("/loltry.php")
Call<NewsList> getNews();
}
NewsList.java:
public class NewsList
{
@SerializedName("news")
ArrayList<Data> d;
}
Data.java:
public class Data
{
@SerializedName("data")
News n;
}
NewsList newsList = response.body();
String rootUrl = "http://192.168.0.5";
Retrofit adapter = new Retrofit.Builder().baseUrl(rootUrl).addConverterFactory(GsonConverterFactory.create()).build();
NewsAPI newsAPI = adapter.create(NewsAPI.class);
final Call<NewsList> call = newsAPI.getNews();
call.enqueue(new Callback<NewsList>()
{
@Override
public void onResponse(Call<NewsList> call, Response<NewsList> response)
{
Toast.makeText(MainActivity.this, "Succcessss", Toast.LENGTH_LONG).show();
newsList = response.body();
try
{
for (int i = 0; i < newsList.d.size(); ++i)
{
newsListArray.add(i, newsList.d.get(i).n.getTitle());
}
} catch (Exception e)
{
e.printStackTrace();
}
}
我的本地主机上的 loltry.php 正在发送您正在尝试解析的 json object。我在列表视图中显示您的 "News"(my "data") object 中的标题。
这是我的 JSONResponse:
{"news":[{"data":{"id":"1","title":"Big data is changing the ace f fashion","description":"Download Fashioning Data: A 2015 Update"}}]}
各位。
我正在尝试使用 retrofit2 连接到 RestApi,但这会给我错误或 return object null。
{"news": [ { "News": { "id": "1", "title": "Big data is changing the ace f fashion", "description": "Download Fashioning Data: A 2015 Update" } }]}
我有下一个代码:
界面休息
@GET("news")
Call<news> getNews();
POJO 对象:
public class news {
@SerializedName("News")
public List<Listnews> listnews;
public static class Listnews{
public News news;
public static class News{
@SerializedName("id")
private String id;
@SerializedName("title")
private String title;
@SerializedName("description")
private String description;
// getters and setters
}
}
并显示在mainActivity
Call<news> call2 = api.getNews();
call2.enqueue(new Callback<news>() {
@Override
public void onResponse(Call<news> call, Response<news> response) {
if (response.code() == 200){
List<news.Listnews> respuesta = response.body().listnews;
System.out.println( "title: "+ respuesta.get(0).news);
}
}
@Override
public void onFailure( Call<news> call, Throwable t ) {
System.out.println( t + " Error " );
}
});
但这曾经return无效。
您的 json 字符串中有两个 "news"、"News"。
由于您的模型使用以下注释
News(数组)
-> id
-> 标题
-> 描述
{
"News": [
{ "id": "1", "title": "Big data is changing the ace f fashion", "description": "Download Fashioning Data: A 2015 Update" },
{ "id": "2", "title": "Big data is changing the ace f fashion", "description": "Download Fashioning Data: A 2015 Update" }
]
}
但是在你的 json 字符串中
{
"news": [{
"News": { "id": "1", "title": "Big data is changing the ace f fashion", "description": "Download Fashioning Data: A 2015 Update" }
}]
}
可以看到结构:
新闻(数组)
->新闻(objects)
->id
->标题
->描述
因此,您无法在其中调整任何数据。
以上是你得到null的原因object,你应该使用第一个json结构来获取新闻数据
以下是对您工作的建议
不要混淆单词"news"和数据结构"News"(http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api) It is okay for you to use "news" as the path to get the list of the news. But it will be painful if you have another "News" or "Listnews" in the return json since it is quite confused to findout which one should be the news. Try another word like "data", "items" or just return an arraylist of the news since your path (http://restful.com/news)通知你正在尝试获取新闻列表
为您的 restful 构建测试用例。如果您尝试测试移动应用程序中的每个 restful api,那将会很痛苦。尝试使用以下
一个。为您构建工具测试 (https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html) 以演示按钮单击以触发 restful
乙。使用 retrofit(https://github.com/square/okhttp/tree/master/mockwebserver) 模拟请求 并将您的 json 字符串复制到转义字符中(使用此网站 http://bernhardhaeussner.de/odd/json-escape/)
这两种方式会节省你大量的开发时间
请在您的 class 中进行以下更改,希望这会奏效。如果没有,请告诉我。
public class news {
// Change 1 ---
@SerializedName("news")
public List<Listnews> listnews;
public static class Listnews{
// Change 2 ---
@SerializedName("News")
public News news;
public static class News{
@SerializedName("id")
private String id;
@SerializedName("title")
private String title;
@SerializedName("description")
private String description;
// getters and setters
} }
这对我有用。我不知道你为什么采取静态 class。我是这样简单地完成的:
POJO class News.java:
public class News
{
String id, title, description;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title = title;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
}
API接口:
public interface NewsAPI
{
@POST("/loltry.php")
Call<NewsList> getNews();
}
NewsList.java:
public class NewsList
{
@SerializedName("news")
ArrayList<Data> d;
}
Data.java:
public class Data
{
@SerializedName("data")
News n;
}
NewsList newsList = response.body();
String rootUrl = "http://192.168.0.5";
Retrofit adapter = new Retrofit.Builder().baseUrl(rootUrl).addConverterFactory(GsonConverterFactory.create()).build();
NewsAPI newsAPI = adapter.create(NewsAPI.class);
final Call<NewsList> call = newsAPI.getNews();
call.enqueue(new Callback<NewsList>()
{
@Override
public void onResponse(Call<NewsList> call, Response<NewsList> response)
{
Toast.makeText(MainActivity.this, "Succcessss", Toast.LENGTH_LONG).show();
newsList = response.body();
try
{
for (int i = 0; i < newsList.d.size(); ++i)
{
newsListArray.add(i, newsList.d.get(i).n.getTitle());
}
} catch (Exception e)
{
e.printStackTrace();
}
}
我的本地主机上的 loltry.php 正在发送您正在尝试解析的 json object。我在列表视图中显示您的 "News"(my "data") object 中的标题。
这是我的 JSONResponse:
{"news":[{"data":{"id":"1","title":"Big data is changing the ace f fashion","description":"Download Fashioning Data: A 2015 Update"}}]}