gpt4 book ai didi

java - Android-改造-java.lang.NullPointerException : Attempt to invoke virtual method

转载 作者:行者123 更新时间:2023-12-02 03:21:48 27 4
gpt4 key购买 nike

我正在尝试在 android 中使用 Retrofit 解析以下 JSON。

{
"message": false,
"suggestions": false,


"vehicle": {
"parked": true,
"uin": "15",
"vin": "WBAEG1312MCB42267",
"make": "Bmw",
"model": "E8SERIES",
"color": "Blue",
"year": "1991",
"package": "Premium",
"options": "",
"interior": "Color: Cream, Type:Leather",
"exterior": "",
"activity": "Parked",
"username": "Dhruba Sarma",
"timestamp": "04-Sep, 00:35",
"latlng": {
"lat": 12.899270164792,
"lng": 77.646080134509
}
}
}

我创建了我的模型类,如下 - VehicleModel.java

public class VehicleModel {

@SerializedName("message")
@Expose
private boolean message;
@SerializedName("suggestions")
@Expose
private boolean suggestions;
@SerializedName("vehicle")
@Expose
private Vehicle vehicle;

public boolean isMessage() {
return message;
}

public boolean isSuggestions() {
return suggestions;
}

public Vehicle getVehicle() {
return vehicle;
}
}

车辆.java

public class Vehicle {

@SerializedName("parked")
@Expose
private boolean parked;
@SerializedName("uin")
@Expose
private String uin;
@SerializedName("vin")
@Expose
private String vin;
@SerializedName("make")
@Expose
private String make;
@SerializedName("model")
@Expose
private String model;
@SerializedName("color")
@Expose
private String color;
@SerializedName("year")
@Expose
private String year;
@SerializedName("package")
@Expose
private String _package;
@SerializedName("options")
@Expose
private String options;
@SerializedName("interior")
@Expose
private String interior;
@SerializedName("exterior")
@Expose
private String exterior;
@SerializedName("activity")
@Expose
private String activity;
@SerializedName("username")
@Expose
private String username;
@SerializedName("timestamp")
@Expose
private String timestamp;
@SerializedName("latlng")
@Expose
private LatLng latlng;

public boolean isParked() {
return parked;
}

public String getUin() {
return uin;
}

public String getVin() {
return vin;
}

public String getMake() {
return make;
}

public String getModel() {
return model;
}

public String getColor() {
return color;
}

public String getYear() {
return year;
}

public String getPackage() {
return _package;
}

public String getOptions() {
return options;
}

public String getInterior() {
return interior;
}

public String getExterior() {
return exterior;
}

public String getActivity() {
return activity;
}

public String getUsername() {
return username;
}

public String getTimestamp() {
return timestamp;
}

public LatLng getLatlng() {
return latlng;
}
}

LatLng.java

public class LatLng {

@SerializedName("lat")
@Expose
private String lat;
@SerializedName("lng")
@Expose
private String lng;

public String getLat() {
return lat;
}

public String getLng() {
return lng;
}
}

VehicleJSONResponse.java

public class VehicleJSONResponse {

private VehicleModel vehicle;

public VehicleModel getVehicleJSONResponse() {
return vehicle;
}
}

这是我如何解析 JSON

Retrofit retrofit = new Retrofit.Builder().baseUrl(baseurl)
.addConverterFactory(GsonConverterFactory.create()).build();

VehicleRequestInterface request = retrofit.create(VehicleRequestInterface.class);
Call<VehicleJSONResponse> call = request.getVehicleJSON(url);
call.enqueue(new Callback<VehicleJSONResponse>() {
@Override
public void onResponse(Call<VehicleJSONResponse> call, Response<VehicleJSONResponse> response) {

VehicleJSONResponse jsonResponse = response.body();

vehicleData = jsonResponse.getVehicleJSONResponse();`

现在的问题是,当我尝试使用下面的代码检索数据时,我得到了预期的消息和建议的值。但我得到vehicle=null检索数据的代码 vehicleData.getVehicle() >>> 这始终为 nullvehicleData.isMessage()>> 正如预期的那样,这是错误的。

任何人都可以在这里帮助我并建议我做错了什么吗?

最佳答案

您正在从服务器获取 VehicleModel json 的响应。因此,将 VehicleRequestInterface 中的 VehicleJsonResponse 替换为 VehicleModel,它应该按预期工作。

关于java - Android-改造-java.lang.NullPointerException : Attempt to invoke virtual method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39498530/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com