2015-10-20 65 views
0

我目前正在尝试对“https://hummingbirdv1.p.mashape.com”进行简单的GET调用,而我正在使用JacksonConverterFactory。我得到的错误是:翻新2反序列化错误

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token 

我一直在阅读栈溢出,似乎它的一个问题,杰克逊和我的POJO没有被正确读取。虽然如果我使用普通的默认Jackson核心而不是改造后的JacksonConverterFactory,它似乎解析得很好。

我的POJO(我离开了所有的parcelable方法和getter和setter)

public class ListItem implements Parcelable { 

private int id; 
private int mal_id; 
private String slug; 
private String status; 
private String url; 
private String title; 
private String alternate_title; 
private int episode_count; 
private int episode_length; 
private String cover_image; 
private String synopsis; 
private String show_type; 
private String started_airing; 
private String finished_airing; 
private float community_rating; 
private String age_rating; 
private ArrayList<Name> genres; 

public ListItem() { 
} 

public ListItem(int id, int mal_id, String slug, String status, String url, String title, String alternate_title, int episode_count, int episode_length, String cover_image, String synopsis, 
       String show_type, String started_airing, String finished_airing, float community_rating, String age_rating, ArrayList<Name> genres) { 
    this.id = id; 
    this.mal_id = mal_id; 
    this.slug = slug; 
    this.status = status; 
    this.url = url; 
    this.title = title; 
    this.alternate_title = alternate_title; 
    this.episode_count = episode_count; 
    this.episode_length = episode_length; 
    this.cover_image = cover_image; 
    this.synopsis = synopsis; 
    this.show_type = show_type; 
    this.started_airing = started_airing; 
    this.finished_airing = finished_airing; 
    this.community_rating = community_rating; 
    this.age_rating = age_rating; 
    this.genres = genres; 
} 

一个例子回应,我想分析的是:

{ 


"id": 1, 
    "mal_id": 1, 
    "slug": "cowboy-bebop", 
    "status": "Finished Airing", 
    "url": "https://hummingbird.me/anime/cowboy-bebop", 
    "title": "Cowboy Bebop", 
    "alternate_title": "", 
    "episode_count": 26, 
    "episode_length": 24, 
    "cover_image": "https://static.hummingbird.me/anime/poster_images/000/000/001/large/hNSma.jpg?1431697256", 
    "synopsis": "Enter a world in the distant future, where Bounty Hunters roam the solar system. Spike and Jet, bounty hunting partners, set out on journeys in an ever struggling effort to win bounty rewards to survive.\r\nWhile traveling, they meet up with other very interesting people. Could Faye, the beautiful and ridiculously poor gambler, Edward, the computer genius, and Ein, the engineered dog be a good addition to the group?", 
    "show_type": "TV", 
    "started_airing": "1998-04-03", 
    "finished_airing": "1999-04-24", 
    "community_rating": 4.48547657328022, 
    "age_rating": "R17+", 
    "genres": [ 
    { 
     "name": "Action" 
    }, 
    { 
     "name": "Adventure" 
    }, 
    { 
     "name": "Comedy" 
    }, 
    { 
     "name": "Drama" 
    }, 
    { 
     "name": "Sci-Fi" 
    }, 
    { 
     "name": "Space" 
    } 
    ] 
} 

使用改装服务:

AnimeRequestService { 
public static String MASHAPE_BASE_URL = "https://hummingbirdv1.p.mashape.com"; 
private static String MASHAPE_DEBUG_KEY = "()*&#()$*)#(&*$)@(#&*$"; 
private final MashapeService mashapeService; 
private final String TAG = AnimeRequestService.class.getCanonicalName(); 


public AnimeRequestService() { 
    OkHttpClient client = new OkHttpClient(); 
    client.networkInterceptors().add(new Interceptor() { 
     @Override 
     public com.squareup.okhttp.Response intercept(Chain chain) throws IOException { 
      chain.request().newBuilder().addHeader("Accept", "application/json"); 
      Request request = chain.request().newBuilder().addHeader("X-Mashape-Key", MASHAPE_DEBUG_KEY).addHeader("accept", "application/json").build(); 
      return chain.proceed(request); 
     } 
    }); 

    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(MASHAPE_BASE_URL) 
      .addConverterFactory(JacksonConverterFactory.create()) 
      .client(client) 
      .build(); 

    mashapeService = retrofit.create(MashapeService.class); 

} 


public interface MashapeService { 
    @GET("/anime/{id}") 
    Call<List<ListItem>> fetchList(@Path("id") int id); 
} 

public void callService(int id) { 
     Call<List<ListItem>> call = mashapeService.fetchList(id); 
     call.enqueue(new Callback<List<ListItem>>() { 
      @Override 
      public void onResponse(Response<List<ListItem>> response, Retrofit retrofit) { 
       for (ListItem listItem : response.body()) { 
        Log.i(TAG, listItem.getTitle()); 
       } 
      } 

      @Override 
      public void onFailure(Throwable t) { 
       Log.i(TAG,t.toString()); 
      } 
     }); 

} 

任何人都可以看到为什么解析会失败从JacksonConverterFactory但不是Jackso核心?

+0

读取异常。 – njzk2

回答

0

它看起来像你试图反序列化一个数组,但你只得到一个单一的json对象。尝试更新您的呼叫以查找对象而不是列表。

public interface MashapeService { 
    @GET("/anime/{id}") 
    Call<ListItem> fetchList(@Path("id") int id); 
} 

public void callService(int id) { 
     Call<ListItem> call = mashapeService.fetchList(id); 
     call.enqueue(new Callback<ListItem>() { 
      @Override 
      public void onResponse(Response<ListItem> response, Retrofit retrofit) { 
       for (ListItem listItem : response.body()) { 
        Log.i(TAG, listItem.getTitle()); 
       } 
      } 

      @Override 
      public void onFailure(Throwable t) { 
       Log.i(TAG,t.toString()); 
      } 
     }); 

} 
+0

非常感谢!这是问题所在。 – cj1098