rharter / auto-value-gson

AutoValue Extension to add Gson De/Serializer support
Apache License 2.0
603 stars 103 forks source link

Failed to invoke public com.ramiissa.dubaiing.data.network.model.news.NewsResponse() with no args #184

Closed alihabbash closed 6 years ago

alihabbash commented 6 years ago

Hi, I have an AutoValue class for newsResponse and RxJava with Retrofit and my AutoValue type adapter is

@AutoValue
public abstract class NewsResponse implements Parcelable {

public static TypeAdapter<NewsResponse> typeAdapter(Gson gson){
    return new AutoValue_NewsResponse.GsonTypeAdapter(gson);
}

@SerializedName("api_text")
public abstract String isSuccess();

@SerializedName("news")
public abstract List<NewsDetails> getNews();

}

my NewsDetails class is

@AutoValue
public abstract class NewsDetails implements Parcelable{

public static TypeAdapter<NewsDetails> typeAdapter(Gson gson){
    return new AutoValue_NewsDetails.GsonTypeAdapter(gson);
}

@SerializedName("id")
public abstract String getNewsId();

@SerializedName("description")
public abstract String getDescription();

@SerializedName("title")
public abstract String getTitle();

@SerializedName("category")
public abstract String getCategory();

@SerializedName("thumbnail")
public abstract String getImageUrl();

@SerializedName("posted_time")
public abstract String getPostedTime();

}

My Rxjava call is

        model.getNews("1", "1", OrderBy.DESC)
            .observeOn(AndroidSchedulers.mainThread())
            .doOnError(throwable -> ToastUtils.showShort(throwable.getMessage()))
            .doOnSuccess(newsResponse -> view.setUpRecyclerView(newsResponse.getNews()))
            .subscribeOn(Schedulers.io())
            .subscribe();

My problem is I got this error when I make a Retrofit call

Failed to invoke public com.ramiissa.dubaiing.data.network.model.news.NewsResponse() with no args

The Api Response from server looks like this

{
"api_status": "200",
"api_text": "success",
"api_version": "1.5.2",
"news": [
    {
        "id": "1",
        "title": "news1news1 news1 news1 news1 news1 news1 news1 news1",
        "description": "news1 news1 news1 news1 news1 news1 news1 news1 news1 news1  news1 news1 news1 news1 news1 news1 news1 news1 news1 news1",
        "category": "2",
        "category_name": "English",
        "thumbnail": "http://www.dubai-ing.net/upload/photos/2018/08/nAlMzZ4xZ3XfdMsyT5bj_05_c908f62a43c4f7c8d04e38b29858d9e1_image.jpg",
        "posted_time": "5 hours ago",
        "tags_array": [
            "news1"
        ]
    }
  ]
}
rharter commented 6 years ago

Have you tegisterd the type adapters with the gsonisntance that you provide to retrofit?

On Sun, Aug 5, 2018, 4:06 PM Ali Habbash notifications@github.com wrote:

Hi, I have an AutoValue class for newsResponse and RxJava with Retrofit and my AutoValue type adapter is

@AutoValue public abstract class NewsResponse implements Parcelable {

public static TypeAdapter typeAdapter(Gson gson){ return new AutoValue_NewsResponse.GsonTypeAdapter(gson); }

@SerializedName("api_text") public abstract String isSuccess();

@SerializedName("news") public abstract List getNews();

}

my NewsDetails class is

@AutoValue public abstract class NewsDetails implements Parcelable{

public static TypeAdapter typeAdapter(Gson gson){ return new AutoValue_NewsDetails.GsonTypeAdapter(gson); }

@SerializedName("id") public abstract String getNewsId();

@SerializedName("description") public abstract String getDescription();

@SerializedName("title") public abstract String getTitle();

@SerializedName("category") public abstract String getCategory();

@SerializedName("thumbnail") public abstract String getImageUrl();

@SerializedName("posted_time") public abstract String getPostedTime();

}

My Rxjava call is

    model.getNews("1", "1", OrderBy.DESC)
        .observeOn(AndroidSchedulers.mainThread())
        .doOnError(throwable -> ToastUtils.showShort(throwable.getMessage()))
        .doOnSuccess(newsResponse -> view.setUpRecyclerView(newsResponse.getNews()))
        .subscribeOn(Schedulers.io())
        .subscribe();

My problem is I got this error when I make a Retrofit call

Failed to invoke public com.ramiissa.dubaiing.data.network.model.news.NewsResponse() with no args

The Api Response from server looks like this

{ "api_status": "200", "api_text": "success", "api_version": "1.5.2", "news": [ { "id": "1", "title": "news1news1 news1 news1 news1 news1 news1 news1 news1", "description": "news1 news1 news1 news1 news1 news1 news1 news1 news1 news1 news1 news1 news1 news1 news1 news1 news1 news1 news1 news1", "category": "2", "category_name": "English", "thumbnail": "http://www.dubai-ing.net/upload/photos/2018/08/nAlMzZ4xZ3XfdMsyT5bj_05_c908f62a43c4f7c8d04e38b29858d9e1_image.jpg", "posted_time": "5 hours ago", "tags_array": [ "news1" ] } ] }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/rharter/auto-value-gson/issues/184, or mute the thread https://github.com/notifications/unsubscribe-auth/ABPJbm1_G5V7L2yqboeM9OEcN7jzaztJks5uN143gaJpZM4VviQr .

alihabbash commented 6 years ago

Thanks It's worked. I forgot to register the type adapter to Retrofit.