java.lang.IllegalArgumentException:无法为

时间:2019-07-16 07:54:46

标签: android gson retrofit2 rx-java2

我在Android项目中使用gson + rxjava2 + retrofit2。我遇到以下错误:

java.lang.IllegalArgumentException: Unable to create converter for
tv.mchang.data.api.Result<tv.mchang.data.api.bean.main.ColumnInfo>

当前仅在Android4.4 HISIV510中。 这个问题是什么原因造成的? 如何解决这个问题呢? 请帮忙。

//Error message
java.lang.IllegalArgumentException: Unable to create converter for tv.mchang.data.api.Result<tv.mchang.data.api.bean.main.ColumnInfo>
for method IMainService.getColumnInfo
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:755)
at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:741)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:172)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
at $Proxy0.getColumnInfo(Native Method)
at tv.mchang.data.api.main.MainAPI.getColumnInfo(MainAPI.java:225)
at tv.mchang.data.repository.ApiResRepo.getMainColumn(ApiResRepo.java:41)
at tv.mchang.main.fragment.BaseFragment.loadData(BaseFragment.java:447)
at tv.mchang.main.fragment.BaseFragment.onActivityCreated(BaseFragment.java:144)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2270)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1442)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2209)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:649)
at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:167)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1238)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1616)
at android.view.View.measure(View.java:16497)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1210)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1550)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2312)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1917)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1114)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1296)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1001)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5719)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native M

//端点声明

    @GET("upgrade/homepage/all/{channelId}/{type}")
    Observable<Result<ColumnInfo>> getColumnInfo(@Path("channelId") String channelId,@Path("type") int type);

//客户端类

    @Provides
    @Singleton
    RxJava2CallAdapterFactory provideCallAdapterFactory() {
       return RxJava2CallAdapterFactory.create();
    }

    @Provides
    @Singleton
    GsonConverterFactory provideConverterFactory(Gson gson) {
            return GsonConverterFactory.create(gson);
    }
    @Provides
    @Singleton
    Retrofit provideRetrofit(@Named("tv_base_url") String baseUrl, OkHttpClient client,RxJava2CallAdapterFactory callAdapterFactory, GsonConverterFactory converterFactory) {
    return new Retrofit.Builder()
    .baseUrl(baseUrl)
    .addCallAdapterFactory(callAdapterFactory)
    .addConverterFactory(converterFactory)
    .build();
}

二手货:

public Observable<ColumnInfo> getColumnInfo(int type) {
        return mService.getColumnInfo(channelId, type)
                .map(new Function<Result<ColumnInfo>, ColumnInfo>() {
                    @Override
                    public ColumnInfo apply(Result<ColumnInfo> columnInfoResult) throws Exception {
//                        Logger.i("columnInfoResult:"+columnInfoResult.toString());
                        return columnInfoResult.getContent();
                    }
                })
                .subscribeOn(mScheduler);
    }

课程结果:

public class Result<T> {

    private String result;

    private T content;

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }

    @Override public String toString() {
        return "Result{" +
                "result='" + result + '\'' +
                ", content=" + content +
                '}';
    }
}

这是ColumnInfo:

@SerializedName("moduleInfos")
List<ColumnItemInfo> mColumnItemInfos;      // 栏目下拉条目列表

public List<ColumnItemInfo> getColumnItemInfos() {
    return mColumnItemInfos;
}

public void setColumnItemInfos(List<ColumnItemInfo> columnItemInfos) {
    mColumnItemInfos = columnItemInfos;
}

@Override
public String toString() {
    return "ColumnInfo{" +
            "mColumnItemInfos=" + mColumnItemInfos +
            '}';
}

和ColumnItemInfo:

@SerializedName("moduleDetailInfos")
List<ColumnBoxInfo> mColumnBoxInfos;
@SerializedName("formId")
int layoutId;       // 布局样式
String name;        // 条目标题

public List<ColumnBoxInfo> getColumnBoxInfos() {
    return mColumnBoxInfos;
}

public void setColumnBoxInfos(List<ColumnBoxInfo> columnBoxInfos) {
    mColumnBoxInfos = columnBoxInfos;
}

public int getLayoutId() {
    return layoutId;
}

public void setLayoutId(int layoutId) {
    this.layoutId = layoutId;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public String toString() {
    return "ColumnItemInfo{" +
            "mColumnBoxInfos=" + mColumnBoxInfos +
            ", layoutId=" + layoutId +
            ", name='" + name + '\'' +
            '}';
}

和ColumnBoxInfo:

int contentType;
long contentId;
String coverPath;
@SerializedName("videoPlayInfo")
VideoInfo videoInfo;
@SerializedName("h5ActUrl")
String h5Url;
String name;
String previewUrl;
@SerializedName("onClick")
int clickEvent;
String userId;
String locationId;
String moduleId;

public  String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public  String getModuleId() {
    return moduleId;
}

public void setModuleId(String moduleId) {
    this.moduleId = moduleId;
}

public String getLocationId() {
    return locationId;
}

public void setLocationId(String locationId) {
    this.locationId = locationId;
}

public int getClickEvent() {
    return clickEvent;
}

public void setClickEvent(int clickEvent) {
    this.clickEvent = clickEvent;
}

public String getPreviewUrl() {
    return previewUrl;
}

public void setPreviewUrl(String previewUrl) {
    this.previewUrl = previewUrl;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getH5Url() {
    return h5Url;
}

public void setH5Url(String h5Url) {
    this.h5Url = h5Url;
}

public int getContentType() {
    return contentType;
}

public void setContentType(int contentType) {
    this.contentType = contentType;
}

public long getContentId() {
    return contentId;
}

public void setContentId(long contentId) {
    this.contentId = contentId;
}

public String getCoverPath() {
    return coverPath;
}

public void setCoverPath(String coverPath) {
    this.coverPath = coverPath;
}

public VideoInfo getVideoInfo() {
    return videoInfo;
}

public void setVideoInfo(VideoInfo videoInfo) {
    this.videoInfo = videoInfo;
}

@Override
public String toString() {
    return "ColumnBoxInfo{" +
            "contentType=" + contentType +
            ", contentId=" + contentId +
            ", coverPath='" + coverPath + '\'' +
            ", videoInfo=" + videoInfo +
            ", h5Url='" + h5Url + '\'' +
            ", name='" + name + '\'' +
            ", previewUrl='" + previewUrl + '\'' +
            ", clickEvent=" + clickEvent +
            '}';
}

// gradle依赖rxjava并使用compileSdk26进行改造

api "com.google.code.gson:gson:2.8.2"
api "com.squareup.okhttp3:okhttp:3.10.0"
// retrofit2
api "com.squareup.retrofit2:retrofit:2.4.0"
api "com.squareup.retrofit2:converter-gson:2.4.0"
api "com.squareup.retrofit2:adapter-rxjava2:2.4.0"
// RxJava
api "io.reactivex.rxjava2:rxjava:2.1.9"
api "io.reactivex.rxjava2:rxandroid:2.0.2"

JSON:

{
    "content": {
        "moduleInfos": [
            {
                "moduleDetailInfos": [
                    {
                        "onClick": "17",
                        "moduleId": "3",
                        "locationId": "1",
                        "coverPath": "156099617565408db16789d6347c2af.jpg",
                        "contentType": "12",
                        "contentId": "182",
                        "previewUrl": "ST_DSSN_20190620"
                    },
                    {
                        "onClick": "18",
                        "moduleId": "3",
                        "locationId": "2",
                        "coverPath": "15560923280430c6255995fd54f3080.png",
                        "contentType": "12",
                        "contentId": "173",
                        "previewUrl": "ST_XBNZ_20190424"
                    },
                    {
                        "onClick": "19",
                        "moduleId": "3",
                        "name": "00后",
                        "locationId": "3",
                        "coverPath": "1560928273336a80f74a13dd848d3bd.png",
                        "contentType": "3",
                        "contentId": "174",
                        "previewUrl": "ST_WYF_20190428"
                    },
                    {
                        "onClick": "20",
                        "moduleId": "3",
                        "name": "90后",
                        "locationId": "4",
                        "coverPath": "156092828996050fe36f0ebd84fb4ba.png",
                        "contentType": "3",
                        "contentId": "175",
                        "previewUrl": ""
                    },
                    {
                        "onClick": "21",
                        "moduleId": "3",
                        "name": "80后",
                        "locationId": "5",
                        "coverPath": "15609283009055d80d99c39a44983b0.png",
                        "contentType": "3",
                        "contentId": "176",
                        "previewUrl": ""
                    }
                ],
                "formId": "1",
                "name": ""
            },
            {
                "moduleDetailInfos": [
                    {
                        "moduleId": "27",
                        "name": "陈粒",
                        "locationId": "6",
                        "coverPath": "1548925880633bbf4ef8113744af3a8.jpg",
                        "contentType": "3",
                        "contentId": "170",
                        "previewUrl": ""
                    },
                    {
                        "moduleId": "27",
                        "name": "田馥甄",
                        "locationId": "7",
                        "coverPath": "15239552809853057f1d713e0469fb2.jpg",
                        "contentType": "3",
                        "contentId": "68",
                        "previewUrl": ""
                    },
                    {
                        "moduleId": "27",
                        "name": "蔡依林",
                        "locationId": "8",
                        "coverPath": "15239552956229f18f7ce57994bec96.jpg",
                        "contentType": "3",
                        "contentId": "69",
                        "previewUrl": ""
                    },
                    {
                        "moduleId": "27",
                        "name": "鹿晗",
                        "locationId": "9",
                        "coverPath": "154892709864369daf9b880374aa8a1.jpg",
                        "contentType": "3",
                        "contentId": "171",
                        "previewUrl": ""
                    },
                    {
                        "moduleId": "27",
                        "name": "张杰",
                        "locationId": "10",
                        "coverPath": "152395532304126d00879315f42d2b2.jpg",
                        "contentType": "3",
                        "contentId": "71",
                        "previewUrl": ""
                    },
                    {
                        "moduleId": "27",
                        "name": "李荣浩",
                        "locationId": "11",
                        "coverPath": "15239553458759b5d6175081b4afbb0.jpg",
                        "contentType": "3",
                        "contentId": "72",
                        "previewUrl": ""
                    },
                    {
                        "moduleId": "27",
                        "name": "乌兰图雅",
                        "locationId": "12",
                        "coverPath": "1548927263159e7e2a9e5971e4d5c83.jpg",
                        "contentType": "3",
                        "contentId": "169",
                        "previewUrl": ""
                    },
                    {
                        "moduleId": "27",
                        "locationId": "13",
                        "contentType": "7",
                        "videoPlayInfo": {}
                    },
                    {
                        "moduleId": "27",
                        "locationId": "14",
                        "contentType": "7",
                        "videoPlayInfo": {}
                    },
                    {
                        "moduleId": "27",
                        "locationId": "15",
                        "contentType": "7",
                        "videoPlayInfo": {}
                    }
                ],
                "formId": "2",
                "name": "音乐人"
            }
        ]
    },
    "result": "ok"
}

0 个答案:

没有答案