Java 改型返回空值

Java 改型返回空值,java,android,retrofit,Java,Android,Retrofit,我正在使用改型,而且我在安卓系统中是一个相当不学无术的人 我的JSON响应 { "movie": { "_id": "568661682d33bca267fdf81b", "poster_path": "https://image.tmdb.org/t/p/w154/xHfhQIK4BNlsGv5Ylx8mVq0hJT1.jpg", "adult": false, "overview": "", "r

我正在使用改型,而且我在安卓系统中是一个相当不学无术的人

我的JSON响应

    {
    "movie": {
        "_id": "568661682d33bca267fdf81b",
        "poster_path": "https://image.tmdb.org/t/p/w154/xHfhQIK4BNlsGv5Ylx8mVq0hJT1.jpg",
        "adult": false,
        "overview": "",
        "release_date": "2015-05-15",
        "id": 76341,
        "original_title": "Mad Max: Fury Road",
        "original_language": "en",
        "title": "Mad Max: Furia en la carretera",
        "vote_count": 3105,
        "__v": 0,
        "popular": true,
        "production_companies": [],
        "cast": [],
        "genre_ids": [
            53,
            28,
            12
        ]
    }
}
这是我的电影模型:

public class Movie {

public String Id;
//more variables

public String getId() {
    return Id;
}

public void setId(String id) {
    Id = id;
}
//more getter and setter
我的改装界面

public interface MyApiEndPointsInterface {
  @GET("/movie/{id}")
  Call<Movie> getMovie(@Path("id") int id);
}
公共接口MyApiEndPointsInterface{
@获取(“/movie/{id}”)
调用getMovie(@Path(“id”)int-id);
}
以及活动中的代码

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

    MyApiEndPointsInterface apiService =
            retrofit.create(MyApiEndPointsInterface.class);

    Call<Movie> call = apiService.getMovie(76341);

    call.enqueue(new Callback<Movie>() {
        @Override
        public void onResponse(Response<Movie> response, Retrofit retrofit) {
            Log.d("MainActivity", response.body().title);
        }

        @Override
        public void onFailure(Throwable t) {

        }
    });
reformation-reformation=new-reformation.Builder()
.baseUrl(基本URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
MyAPI端点接口API服务=
create(myapidentpointsinterface.class);
Call Call=apiService.getMovie(76341);
call.enqueue(新回调(){
@凌驾
公共响应(响应、改装){
Log.d(“MainActivity”,response.body().title);
}
@凌驾
失效时的公共无效(可丢弃的t){
}
});
我认为它可以是JSON响应,因为它有一个更好的实例,但我不知道如何在代码中使用它

我没有得到回应,一切正常吗?

将电影模式更改为:

public class Movie { 

    @SerializedName("_id")
    public String id;
    //more variables 

    public String getId() {
        return Id;
    } 

    public void setId(String id) {
        id = id;
    }
}

Gson需要确切地知道如何通过反射映射模型。如果您没有另外说明,它主要使用您的变量名。

您的字符串名对它们在JSON输出中的显示方式是区分大小写的。将
String Id更改为String Id
,它应该可以工作,这在getter和setter名称中并不重要(例如
getId
可以工作),但变量本身需要与JSON中的大小写匹配。

问题是JSON解析器不希望使用未命名的根对象在JSON中包装“电影”数据

如果可以更改服务器返回的JSON,请删除外部对象,以便

{
    "_id": "568661682d33bca267fdf81b",
    "poster_path": "https://image.tmdb.org/t/p/w154/xHfhQIK4BNlsGv5Ylx8mVq0hJT1.jpg",
    "adult": false,
    "overview": "",
    "release_date": "2015-05-15",
    "id": 76341,
    "original_title": "Mad Max: Fury Road",
    "original_language": "en",
    "title": "Mad Max: Furia en la carretera",
    "vote_count": 3105,
    "__v": 0,
    "popular": true,
    "production_companies": [],
    "cast": [],
    "genre_ids": [
        53,
        28,
        12
    ]
}
如果无法更改服务器的响应,有几种方法可以在客户端处理。我只需创建另一个对象来映射到外部JSON对象,因此保持现有的Movie类不变,添加:

public class MovieResponse {
 public Movie movie;
}
然后将改装调用更改为使用MovieResponse而不是Movie。 然后您可以参考

movieResponse.movie

我通过改变API解决了这个问题,这是JSON的第一个属性,非常感谢:)