如何在Retrofit中处理以下JSON?给错误无效的双倍?

时间:2017-01-24 14:41:33

标签: java android json retrofit2 gson

{
"id": "297761",
"results": [{
        "id": "58403fa09251417f7400b62a",
        "iso_639_1": "en",
        "iso_3166_1": "US",
        "key": "CmRih_VtVAs",
        "name": "Official Trailer #1",
        "site": "YouTube",
        "size": 1080,
        "type": "Trailer"
   }]
}

错误:com.google.gson.JsonSyntaxException:java.lang.NumberFormatException:无效的双倍:“58403fa09251417f7400b62a”

以下是解析它的代码:

@SerializedName("id")private String mId;
@SerializedName("iso_639_1")private String mLanguage;
@SerializedName("iso_3166_1")private String mLanguage2;
@SerializedName("key")private String mKey;
@SerializedName("name")private String mName;
@SerializedName("site")private String mSite;
@SerializedName("size")private Integer mSize;
@SerializedName("type")private String mType;

    @Override 
public void writeToParcel(Parcel parcel, int i) 
{ 
    parcel.writeString(mId); 
    parcel.writeString(mLanguage); 
    parcel.writeString(mKey); 
    parcel.writeString(mName); 
    parcel.writeString(mSite); 
    parcel.writeInt(mSize); 
    parcel.writeString(mType); 
} 

验证显示正确。在Java中使用SerializedNames的Arraylist以字符串格式存储数据,除了“size”属性是整数。

改造方法: -

  private ArrayList<MovieTrailer> getTrailers(String id) {
    ApiInterface apiService =
            ApiClient.getClient().create(ApiInterface.class);
    Call<MovieResponse> call = null;
    call = apiService.getMovieTrailers(id,BuildConfig.THE_MOVIE_DB_API_KEY);
    call.enqueue(new Callback<MovieResponse>() {
        @Override
        public void onResponse(Call<MovieResponse> call, Response<MovieResponse> response) {   
         movieTrailers = (ArrayList<MovieTrailer>) response.body().getTrailerResults();
           Log.d(TAG, "server contacted at: " + call.request().url());


        }

        @Override
        public void onFailure(Call<MovieResponse> call, Throwable t) {
            // Log error here since request failed
            Log.d(TAG, "server contacted at: " + call.request().url());
            Log.e(TAG, t.toString());
        }
    });
    return movieTrailers;
}

1 个答案:

答案 0 :(得分:0)

由于改造返回了网址

的整个JSON结果

我使用serializable来浏览所有值:

@SerializedName("id")
private int id;
@SerializedName("results")
private List<MovieTrailerResults> movieTrailerResults;

然后在MovieTrailerResults类中添加以下给出变量并且它可以正常工作。

@SerializedName("id")private String mId;
@SerializedName("iso_639_1")private String mLanguage;
@SerializedName("iso_3166_1")private String mLanguage2;
@SerializedName("key")private String mKey;
@SerializedName("name")private String mName;
@SerializedName("site")private String mSite;
@SerializedName("size")private Integer mSize;
@SerializedName("type")private String mType;