Retrofit返回数据,但pojo对象始终为空

时间:2016-11-02 02:00:13

标签: android json web-services retrofit

我试图通过改造来使用服务2.当我使用网络浏览器调用它或使用拦截器记录调用时,会有一些结果,但我的pojo类总是空的。

json结果内容类似于:

  {
      "similarartists": {
        "artist": [
        {
          "name": "Barão Vermelho",
          "mbid": "84ac395b-482b-48cb-b381-b9bc420b2dd3",
          "match": "1",
          "url": "https://www.last.fm/music/Bar%C3%A3o+Vermelho",
          "image": [],
          "streamable": "0"
        },
        "@attr": {
          "artist": "Cazuza"
        }
     }
  }

这些是我的pojo课程:

public class Similarartists {

    @SerializedName("artist")
    private List<Artist> artist = new ArrayList<>();

    @SerializedName("attr")
    private Attr attr;

}

public class Artist {

    @SerializedName("name")
    private String name;

    @SerializedName("mbid")
    private String mbid;

    @SerializedName("match")
    private String match;

    @SerializedName("url")
    private String url;

    @SerializedName("image")
    private List<Object> image = new ArrayList<>();

    @SerializedName("streamable")
    private String streamable;
}

public class Attr {
    @SerializedName("artist")
    private String artist;
}

服务连接类:

public class ApiService {
    private static final String BASE_URL = "http://ws.audioscrobbler.com/";
    public static final String API_KEY = "XXXXXXXXXX";

    private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
            .readTimeout(30, TimeUnit.SECONDS)
            .connectTimeout(30, TimeUnit.SECONDS);

    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create());

    public static <S> S createService(Class<S> serviceClass) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        httpClient.addInterceptor(logging);

        httpClient.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request original = chain.request();

                // Request customization: add request headers
                Request.Builder requestBuilder = original.newBuilder()
                        .header("Accept", "application/json")
                        .header("Content-Type", "application/json")
                        .method(original.method(), original.body());

                Request request = requestBuilder.build();
                return chain.proceed(request);
            }
        });

        Retrofit retrofit = builder.client(httpClient.build()).build();

        return retrofit.create(serviceClass);
    }

}

这是我的&#34;按钮&#34;动作:

private void openSearchDialog() {
    ArtistService artistService = ApiService.createService(ArtistService.class);
    Call<Similarartists> call = artistService.getSimilar("nirvana", ApiService.API_KEY);
    call.enqueue(new Callback<Similarartists>() {
        @Override
        public void onResponse(Call<Similarartists> call, Response<Similarartists> response) {
            if (response.isSuccessful()) {
                mAdapter.setValues(response.body().getArtist());
                Log.i(TAG, "onResponse: " + response.body().toString());
            }
        }

        @Override
        public void onFailure(Call<Similarartists> call, Throwable t) {
            Log.e(TAG, "onFailure: ", t);
        }
    });
}

问题是response.body()。getArtist()总是空的。请帮帮我。

这是我的日志结果:

-01 22:51:58.844 23843-24043 / com.sample.pablo.hellomusic D / OkHttp:{&#34; similarartists&#34;:{&#34; artist&#34;:[{&#34 ;名称&#34;:&#34;空穴&#34;&#34; MBID&#34;:&#34; 1dcc8968-f2cd-441C-贝达-6270f70f2863&#34;&#34;匹配&#34 ;: #&34; 1&#34;&#34; URL&#34;:&#34; https://www.last.fm/music/Hole&#34;&#34;图像&#34;:[{&#34;#文本&# 34;:&#34; https://lastfm-img2.akamaized.net/i/u/34s/6687f63408074388ae703eb3905e238f.png&#34;&#34;大小&#34;:&#34;小&#34;},{&#34;#文本&#34;:&#34 ; https://lastfm-img2.akamaized.net/i/u/64s/6687f63408074388ae703eb3905e238f.png&#34;&#34;大小&#34;:&#34;介质&#34;},{&#34;#文本&#34;:&#34; https://lastfm-img2.akamaized.net/i/u/174s/6687f63408074388ae703eb3905e238f.png &#34;&#34;大小&#34;:&#34;大&#34;},{&#34;#文本&#34;:&#34; https://lastfm-img2.akamaized.net/i/u/300x300/6687f63408074388ae703eb3905e238f.png&#34;,& #34;大小&#34;:&#34;海&#34;},{&#34;#文本&#34;:&#34; https://lastfm-img2.akamaized.net/i/u/6687f63408074388ae703eb3905e238f.png&#34;&#34;大小&#34 ;:&#34;巨型&#34;},{&#34;#文本&#34;:&#34; https://lastfm-img2.akamaized.net/i/u/arQ/6687f63408074388ae703eb3905e238f.png&#34;&#34;大小&#34;:&#34; &#34;}],&#34;流传送的&#34;:&#34; 0&#34;}],&#34; @ ATTR&#34; {&#34;艺术家&#34;:&#34 ;涅&#34;}}}

1 个答案:

答案 0 :(得分:1)

由于响应JSON中存在similarartists的顶级键,我认为您需要围绕此对象的另一个包装器,例如:

public class SimilarArtistsResponse {
  @SerializedName("similarartists")
  private Similarartists similars;
}

您要在Call<SimilarArtistsResponse>

中指定此对象