我的 Java 对象构造函数返回“null”
My Java object constructor returning “null”
我创建了一个 class,其中包含各种 Activity 的构造函数。但是,当我尝试订阅该对象以显示产品的一些图像、价格和名称时,我收到 "null" 作为输出。但是当我尝试在 else 语句中传递相同的函数时它起作用但仍然返回 null。请帮助我理解为什么会出现此错误。并告诉我如何解决 在此先感谢。
mCompositeDisposable.add(mIMyRestaurantAPI.getFoodOfMenu(Common.API_KEY,
event.getCategory().getId())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(foodModel -> {
if (foodModel.isSuccess()) {
adapter = new MyFoodAdapter(this, foodModel.getResult());
recycler_food_list.setAdapter(adapter);
recycler_food_list.setLayoutAnimation(mLayoutAnimationController);
} else {
Toast.makeText(this, "[GET FOOD RESULT]" + foodModel.getMessage(), Toast.LENGTH_LONG).show();
}
mDialog.dismiss();
}, throwable -> {
mDialog.dismiss();
Toast.makeText(this, "[GET FOOD]" + throwable.getMessage(), Toast.LENGTH_SHORT).show();
}));
我不断收到 GET FOOD RESULT=null..这就是我的 FOODMODEL 构造器的样子
public class FoodModel {
private boolean success;
private String message;
private List<Food> result;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<Food> getResult() {
return result;
}
public void setResult(List<Food> result) {
this.result = result;
}
这是 FOOD 构造函数的样子
public class Food {
private int id;
private String name;
private String description;
private String image;
private Double price;
private boolean isSize;
private boolean isAddon;
private int discount;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public boolean isSize() {
return isSize;
}
public void setSize(boolean size) {
isSize = size;
}
public boolean isAddon() {
return isAddon;
}
public void setAddon(boolean addon) {
isAddon = addon;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
这是我使用改造从后端传递连接的地方。
@GET("food")
Observable<FoodModel> getFoodOfMenu(@Query("key") String apiKey,
@Query("menuId") int menuId);
这是我使用 nodejs 的 api 后端..
router.get('/food', function (req, res, next) {
if (req.query.key == API_KEY) {
req.getConnection(function (error, conn) {
var menu_id = req.query.menuId
if (menu_id != null) {
conn.query('SELECT id,name,description,image,price, CASE WHEN isSize=1 THEN \'TRUE\' ELSE \'FALSE\' END as isSize,'
+'CASE WHEN isAddon=1 THEN \'TRUE\' ELSE \'FALSE\' END as isAddon,'
+ 'discount FROM Food WHERE Id in (SELECT foodId FROM Menu_Food WHERE MenuId=?)', [menu_id], function (err, rows, fields) {
if (err) {
res.status(500)
res.send(JSON.stringify({ sucess: false, message: err.message }))
}
else {
if (rows.length > 0) {
res.send(JSON.stringify({ sucess: true, result: rows }))
}
else {
res.send(JSON.stringify({ sucess: false, message: "Empty" }))
}
}
})
} else {
res.send(JSON.stringify({ sucess: false, message: "Missing menu_id" }))
}
})
}
else {
res.send(JSON.stringify({ success: false, message: "Wrong api key marfe" }))
}
})
这里是 json 回复
{"sucess":true,"result":[{"id":1,"name":"ROASTED QUARTER CHICKEN","description":"Served with mushroom gravy, cranberry & mint sauces","image":"http://10.0.2.2:3000/4_roasted_quarter_chicken_with_special_sauces.jpg","price":25,"isSize":"FALSE","isAddon":"FALSE","discount":0},{"id":2,"name":"CURRY CHICKEN","description":"Chicken served in curry that is made from more than 10 spices to bring out the authentic traditional taste. Served with rice and 3 side dishes\r\n","image":"http://10.0.2.2:3000/2_curry_chic.jpg","price":25,"isSize":"FALSE","isAddon":"FALSE","discount":0},{"id":3,"name":"\r\nRENDANG CHICKEN","description":"Simmered chicken in spices. Served with rice and 3 side dishes","image":"http://10.0.2.2:3000/1_classic_rendang.jpg","price":25,"isSize":"FALSE","isAddon":"FALSE","discount":0},{"id":4,"name":"HERBAL STEAMED CHICKEN","description":"Steamed chicken with red dates and mushroom. Served with rice and 3 side dishes","image":"http://10.0.2.2:3000/3_herbal_steamed_chic.jpg","price":25,"isSize":"FALSE","isAddon":"FALSE","discount":0}]}
您必须从 api 响应中得到 null。
或者您的 Java 对象及其字段应与您的 json 响应中的属性相匹配。
例如你有这个json{
"countries": [
{
"name": "Israel",
"phoneCode": "972",
"code": "IL"
},
{
"name": "Afghanistan",
"phoneCode": "93",
"code": "AF"
},
{
"name": "Albania",
"phoneCode": "355",
"code": "AL"
}]}
那么你的模型就是这样
public class CountryModel {
private List<Country> countries = new ArrayList<>();
public List<Country> getCountries() {
return countries;
}
public void setCountries(List<Country> countries) {
this.countries = countries;
}
public static class Country {
private String name;
private String code;
private String phoneCode;
public Country(String name, String code, String phoneCode) {
this.name = name;
this.code = code;
this.phoneCode = phoneCode;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneCode() {
return phoneCode;
}
public void setPhoneCode(String phoneCode) {
this.phoneCode = phoneCode;
}
}
}
你的 json 你的 Java 模型应该是这样的
public class Example {
private Boolean sucess;
private List<Result> result = null;
public Boolean getSucess() {
return sucess;
}
public void setSucess(Boolean sucess) {
this.sucess = sucess;
}
public List<Result> getResult() {
return result;
}
public void setResult(List<Result> result) {
this.result = result;
}
}
我创建了一个 class,其中包含各种 Activity 的构造函数。但是,当我尝试订阅该对象以显示产品的一些图像、价格和名称时,我收到 "null" 作为输出。但是当我尝试在 else 语句中传递相同的函数时它起作用但仍然返回 null。请帮助我理解为什么会出现此错误。并告诉我如何解决 在此先感谢。
mCompositeDisposable.add(mIMyRestaurantAPI.getFoodOfMenu(Common.API_KEY,
event.getCategory().getId())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(foodModel -> {
if (foodModel.isSuccess()) {
adapter = new MyFoodAdapter(this, foodModel.getResult());
recycler_food_list.setAdapter(adapter);
recycler_food_list.setLayoutAnimation(mLayoutAnimationController);
} else {
Toast.makeText(this, "[GET FOOD RESULT]" + foodModel.getMessage(), Toast.LENGTH_LONG).show();
}
mDialog.dismiss();
}, throwable -> {
mDialog.dismiss();
Toast.makeText(this, "[GET FOOD]" + throwable.getMessage(), Toast.LENGTH_SHORT).show();
}));
我不断收到 GET FOOD RESULT=null..这就是我的 FOODMODEL 构造器的样子
public class FoodModel {
private boolean success;
private String message;
private List<Food> result;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<Food> getResult() {
return result;
}
public void setResult(List<Food> result) {
this.result = result;
}
这是 FOOD 构造函数的样子
public class Food {
private int id;
private String name;
private String description;
private String image;
private Double price;
private boolean isSize;
private boolean isAddon;
private int discount;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public boolean isSize() {
return isSize;
}
public void setSize(boolean size) {
isSize = size;
}
public boolean isAddon() {
return isAddon;
}
public void setAddon(boolean addon) {
isAddon = addon;
}
public int getDiscount() {
return discount;
}
public void setDiscount(int discount) {
this.discount = discount;
}
这是我使用改造从后端传递连接的地方。
@GET("food")
Observable<FoodModel> getFoodOfMenu(@Query("key") String apiKey,
@Query("menuId") int menuId);
这是我使用 nodejs 的 api 后端..
router.get('/food', function (req, res, next) {
if (req.query.key == API_KEY) {
req.getConnection(function (error, conn) {
var menu_id = req.query.menuId
if (menu_id != null) {
conn.query('SELECT id,name,description,image,price, CASE WHEN isSize=1 THEN \'TRUE\' ELSE \'FALSE\' END as isSize,'
+'CASE WHEN isAddon=1 THEN \'TRUE\' ELSE \'FALSE\' END as isAddon,'
+ 'discount FROM Food WHERE Id in (SELECT foodId FROM Menu_Food WHERE MenuId=?)', [menu_id], function (err, rows, fields) {
if (err) {
res.status(500)
res.send(JSON.stringify({ sucess: false, message: err.message }))
}
else {
if (rows.length > 0) {
res.send(JSON.stringify({ sucess: true, result: rows }))
}
else {
res.send(JSON.stringify({ sucess: false, message: "Empty" }))
}
}
})
} else {
res.send(JSON.stringify({ sucess: false, message: "Missing menu_id" }))
}
})
}
else {
res.send(JSON.stringify({ success: false, message: "Wrong api key marfe" }))
}
})
这里是 json 回复
{"sucess":true,"result":[{"id":1,"name":"ROASTED QUARTER CHICKEN","description":"Served with mushroom gravy, cranberry & mint sauces","image":"http://10.0.2.2:3000/4_roasted_quarter_chicken_with_special_sauces.jpg","price":25,"isSize":"FALSE","isAddon":"FALSE","discount":0},{"id":2,"name":"CURRY CHICKEN","description":"Chicken served in curry that is made from more than 10 spices to bring out the authentic traditional taste. Served with rice and 3 side dishes\r\n","image":"http://10.0.2.2:3000/2_curry_chic.jpg","price":25,"isSize":"FALSE","isAddon":"FALSE","discount":0},{"id":3,"name":"\r\nRENDANG CHICKEN","description":"Simmered chicken in spices. Served with rice and 3 side dishes","image":"http://10.0.2.2:3000/1_classic_rendang.jpg","price":25,"isSize":"FALSE","isAddon":"FALSE","discount":0},{"id":4,"name":"HERBAL STEAMED CHICKEN","description":"Steamed chicken with red dates and mushroom. Served with rice and 3 side dishes","image":"http://10.0.2.2:3000/3_herbal_steamed_chic.jpg","price":25,"isSize":"FALSE","isAddon":"FALSE","discount":0}]}
您必须从 api 响应中得到 null。 或者您的 Java 对象及其字段应与您的 json 响应中的属性相匹配。
例如你有这个json{
"countries": [
{
"name": "Israel",
"phoneCode": "972",
"code": "IL"
},
{
"name": "Afghanistan",
"phoneCode": "93",
"code": "AF"
},
{
"name": "Albania",
"phoneCode": "355",
"code": "AL"
}]}
那么你的模型就是这样
public class CountryModel {
private List<Country> countries = new ArrayList<>();
public List<Country> getCountries() {
return countries;
}
public void setCountries(List<Country> countries) {
this.countries = countries;
}
public static class Country {
private String name;
private String code;
private String phoneCode;
public Country(String name, String code, String phoneCode) {
this.name = name;
this.code = code;
this.phoneCode = phoneCode;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneCode() {
return phoneCode;
}
public void setPhoneCode(String phoneCode) {
this.phoneCode = phoneCode;
}
}
}
你的 json 你的 Java 模型应该是这样的
public class Example {
private Boolean sucess;
private List<Result> result = null;
public Boolean getSucess() {
return sucess;
}
public void setSucess(Boolean sucess) {
this.sucess = sucess;
}
public List<Result> getResult() {
return result;
}
public void setResult(List<Result> result) {
this.result = result;
}
}