意外的JDWP错误:103。Retrofit(2.3.0)GET调用期间发生异常

时间:2018-10-14 19:16:52

标签: kotlin retrofit2 dagger-2

在调用vk.api来获取某些数据期间,我收到意外的JDWP错误:103。

我发现this topic存在相关问题,但是我的申请中已经应用了该建议。

所以也许我的改装配置是错误的?

下面是一些代码:

使用匕首的用于DI的模块

@Module
class NetworkModule {

    @Provides
    internal fun provideRetrofit(): Retrofit {
        return Retrofit.Builder()
                .baseUrl(ApiConstants.VK_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
    }

    @Provides
    internal fun provideGroupApi(retrofit: Retrofit) : GroupApi {
        return retrofit.create(GroupApi::class.java)
    }
}

Api界面:

interface GroupApi {

    @GET(ApiMethods.SEARCH_GROUPS)
    fun getGroups(@QueryMap map: Map<String, String?>) : Observable<GroupResponse>
}

object ApiMethods {

    const val SEARCH_GROUPS = "groups.search"
}

内部查询:

enter image description here

模型类:

data class Response<T>(
        val count: Int,
        val items: List<T>
)

data class GroupResponse(
        @SerializedName("response")
        @Expose
        val response: Response<Group>
)

data class Group(
        @SerializedName("id")
        @Expose
        val id: Int,
        @SerializedName("name")
        @Expose
        val name: String,
        @SerializedName("screenName")
        @Expose
        val screen_name: String,
        @SerializedName("isClosed")
        @Expose
        val is_closed: Int,
        @SerializedName("type")
        @Expose
        val type: String,
        @SerializedName("isAdmin")
        @Expose
        val is_admin: Int,
        @SerializedName("isMebmer")
        @Expose
        val is_member: Int,
        @SerializedName("photo_50")
        @Expose
        val photo_50: String,
        @SerializedName("photo_100")
        @Expose
        val photo_100: String,
        @SerializedName("photo_200")
        @Expose
        val photo_200: String
)

这是vk.api的响应示例(我提供了此示例,因为我认为我的模型配置不正确):

 {
    "response": {
    "count": 193738,
    "items": [{
    "id": 26667550,
    "name": "ARTY",
    "screen_name": "arty_music",
    "is_closed": 0,
    "type": "page",
    "is_admin": 0,
    "is_member": 0,
    "photo_50": "https://pp.vk.me/...841/1B4wTxXinAc.jpg",
    "photo_100": "https://pp.vk.me/...840/Xc_3PikLQ_M.jpg",
    "photo_200": "https://pp.vk.me/...83e/kGwRLtSLJOU.jpg"
    }, {
    "id": 25597207,
    "name": "Alexander Popov",
    "screen_name": "popov.music",
    "is_closed": 0,
    "type": "page",
    "is_admin": 0,
    "is_member": 0,
    "photo_50": "https://pp.vk.me/...e8f/g2Z9jU6qXVk.jpg",
    "photo_100": "https://pp.vk.me/...e8e/DtYBYKLU810.jpg",
    "photo_200": "https://pp.vk.me/...e8d/QRVqdhTvQ4w.jpg"
    }, {
    "id": 42440233,
    "name": "Музыка",
    "screen_name": "exp.music",
    "is_closed": 0,
    "type": "page",
    "is_admin": 0,
    "is_member": 0,
    "photo_50": "https://pp.vk.me/...2d1/52gY6m5ZObg.jpg",
    "photo_100": "https://pp.vk.me/...2d0/Jx9DWph_3ag.jpg",
    "photo_200": "https://pp.vk.me/...2ce/qsFhk6yEtDc.jpg"
    }]
   }
 }

有人可以提供任何建议吗?

更新:

我还尝试了以下另一种响应模型:

data class Root<T> (
    @SerializedName("response")
    @Expose
    val response: T
    )

interface GroupApi {

    @GET(ApiMethods.SEARCH_GROUPS)
    fun getGroups(@QueryMap map: Map<String, String?>) : Observable<Root<Response<Group>>>
}

但还是没有运气...

附加代码:

我称为交互器的演示者->而在内部交互器中,我称为GroupApi:

class SearchResultPresenter<V : SearchResultMVPView, I : SearchResultMVPInteractor> @Inject constructor(interactor: I, schedulerProvider: SchedulerProvider, compositeDisposable: CompositeDisposable)
    : BasePresenter<V, I>(interactor = interactor, schedulerProvider = schedulerProvider, compositeDisposable = compositeDisposable), SearchResultMVPPresenter<V, I> {

    override fun searchGroups(q: String) {
        getView()?.showProgress()
        interactor?.let {
            compositeDisposable.add(it.getGroupList(q)
                    .compose(schedulerProvider.ioToMainObservableScheduler())
                    .subscribe { groupResponse ->
                        getView()?.let {
                            it.showSearchResult(groupResponse.response.items)
                            it.hideProgress()
                        }
                    })
        }
    }
}

class SearchResultInteractor @Inject constructor() : SearchResultMVPInteractor {

    @Inject
    lateinit var groupApi: GroupApi

    override fun getGroupList(q: String): Observable<Root<Response<Group>>> = groupApi.getGroups(GroupRequest(q).toMap())

}

我决定在应用DI时提供完整的代码:

@Singleton
@Component(modules = [(AndroidInjectionModule::class), (AppModule::class), (ActivityBuilder::class)])
interface AppComponent {

    @Component.Builder
    interface Builder {

        @BindsInstance
        fun application(application: Application): Builder

        fun build(): AppComponent
    }

    fun inject(app: MyApplication)
}

片段模块:

@Module
class SearchResultFragmentModule {

    @Provides
    internal fun provideSearchResultInteractor(interactor: SearchResultInteractor): SearchResultMVPInteractor = interactor

    @Provides
    internal fun provideSearchResultFragment(presenter: SearchResultPresenter<SearchResultMVPView, SearchResultMVPInteractor>)
            : SearchResultMVPPresenter<SearchResultMVPView, SearchResultMVPInteractor> = presenter

    @Provides
    internal fun provideSearchResultProvider(): SearchResultAdapter = SearchResultAdapter(ArrayList())

    @Provides
    internal fun provideLayoutManager(fragment: SearchResultFragment) : LinearLayoutManager = LinearLayoutManager(fragment.activity)
}

提供者:

@Module
abstract class SearchResultFragmentProvider {

    @ContributesAndroidInjector(modules = [(SearchResultFragmentModule::class), (NetworkModule::class)])
    internal abstract fun proviceSearchResultFragmentModule(): SearchResultFragment
}

其中包含片段的注射器的活动:

class MainActivity : BaseActivity(), MainMVPView, HasSupportFragmentInjector {

    @Inject
    internal lateinit var presenter: MainMVPPresenter<MainMVPView, MainMVPInteractor>
    @Inject
    internal lateinit var fragmentDispatchingAndroidInjector: DispatchingAndroidInjector<Fragment>

...
//some code
override fun supportFragmentInjector(): AndroidInjector<Fragment> = fragmentDispatchingAndroidInjector
}

活动构建器:

@Module
abstract class ActivityBuilder {

    @ContributesAndroidInjector(modules = [(MainActivityModule::class), (SearchResultFragmentProvider::class)])
    abstract fun bindMainActibity(): MainActivity
}

AppComponent:

@Singleton
@Component(modules = [(AndroidInjectionModule::class), (AppModule::class), (ActivityBuilder::class)])
interface AppComponent {

    @Component.Builder
    interface Builder {

        @BindsInstance
        fun application(application: Application): Builder

        fun build(): AppComponent
    }

    fun inject(app: MyApplication)
}

2 个答案:

答案 0 :(得分:0)

您是否尝试注入改造(而不是GroupApi),然后致电
retrofit.create(GroupApi :: class.java).getGroups(GroupRequest(q).toMap()) 在您的SearchResultInteractor中?另外,您可以将有趣的ProvideRetrofit()注释为Singleton。

答案 1 :(得分:0)

如果仍然有人在看这篇文章-很抱歉,我犯了一个错误。

Retrofit正常运行,问题出在presenter类的未初始化视图中,因此当我在调试中调用api方法groupApi.getGroups(GroupRequest(q).toMap())时-出现异常。但是问题出在视图类上。