我正在尝试将我的本地主机与Retrofit2连接(发送电话号码并获取验证码)。这是我的代码:
override fun requestCode(phone: String) {
disposables.add(
authServices.requestCode(phone)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onNext = {
showMessage(it.message)
},
onError = {
Timber.e(it.printStackTrace().toString())
}
)
)
}
这是我的authServices接口:
interface AuthServices {
@POST("auth/request-code")
@FormUrlEncoded
fun requestCode(@Field("phoneNumber") phoneNumber: String): Observable<ApiResponse<String?>>
}
我的Retrofit对象和依赖项(内部Dagger模块):
@Module
class ApiModule {
@Provides fun authServices(retrofit: Retrofit): AuthServices {
return retrofit.create(AuthServices::class.java)
}
@Provides
fun retrofit(okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder()
.baseUrl("http://10.0.2.2/api/")
.client(okHttpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build()
}
@Provides
fun okHttpClient(
loggingInterceptor: HttpLoggingInterceptor,
sharedPreferences: SharedPreferences,
): OkHttpClient {
return OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.addInterceptor {
val request = it.request().newBuilder()
.addHeader(
"Authorization",
"Bearer ${sharedPreferences.getString(Keys.Prefs.TOKEN, null)}"
)
.build()
return@addInterceptor it.proceed(request)
}
.build()
}
@Provides
fun loggingInterceptor(): HttpLoggingInterceptor {
return HttpLoggingInterceptor(HttpLoggingInterceptor.Logger {
Timber.i(it)
})
}
}
这应该是响应:
{
"message": "Code is: 4588",
"data": null
}
但我在onError内收到此错误:
07-29 16:18:43.689 16822-16822/com.shimibox.client.debug W/System.err: retrofit2.adapter.rxjava2.HttpException: HTTP 403 Request blocked by Privoxy
at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:54)
at retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:37)
at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:44)
at io.reactivex.Observable.subscribe(Observable.java:11194)
at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
at io.reactivex.Observable.subscribe(Observable.java:11194)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:463)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
07-29 16:18:43.689 16822-16822/com.shimibox.client.debug E/LoginPresenter$requestCode: kotlin.Unit
那是什么问题?我什至都不知道Privoxy是什么!!