在调用vk.api来获取某些数据的过程中,我遇到了意外的JDWP错误:103。
我发现this topic存在相关问题,但是从那里提出的建议已经应用到我的应用程序中。
所以也许我的 retrofit 配置错了?
这里有一些代码:
使用 Dagger 的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"
}
内部查询:
模型类别:
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
}
包含注入(inject)器的内部 Activity 的碎片:
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
}
和 Activity 生成器:
@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)
}
最佳答案
您是否尝试注入(inject)改造(而不是GroupApi),然后致电
retrofit.create(GroupApi::class.java).getGroups(GroupRequest(q).toMap())
在您的SearchResultInteractor中?另外,您还可以将有趣的ProvideRetrofit()注释为Singleton。