retrofit2.adapter.rxjava2.httpexception http 403禁止为什么在Kotlin android中发生此错误

时间:2019-12-10 06:06:04

标签: android kotlin retrofit2 okhttp kotlin-android-extensions

我无法连接到服务器,无法从服务器发送或接收数据,此错误使我困扰了很长时间。

这是我的kotlin项目中发生的错误图像

enter image description here

我的改装代码是

package com.epaysave.network

import com.epaysave.BuildConfig
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

open class BaseRetrofit : BaseOKHttp() {

    private fun gsonBuilder(): Gson {
        return GsonBuilder()
            .setLenient()
            .create()
    }

    fun buildRetrofit(): Retrofit {
        return Retrofit.Builder()

            .client(provideOKHttpClient())
            .addConverterFactory(GsonConverterFactory.create(gsonBuilder()))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .baseUrl(BuildConfig.base_url)
            .build()
    }
}

然后我的HTTP代码是

package com.epaysave.network

import com.epaysave.BuildConfig
import okhttp3.*
import okhttp3.logging.HttpLoggingInterceptor
import java.io.IOException
import java.util.concurrent.TimeUnit

open class BaseOKHttp {
    companion object {
        const val NETWORK_CONNECTION_TIMEOUT = 1L
        const val NETWORK_READ_TIMEOUT = 30L
        const val NETWORK_WRITE_TIMEOUT = 15L
    }

    fun provideOKHttpClient(): OkHttpClient {
        val okHttpClient = OkHttpClient.Builder()
        okHttpClient
            .connectTimeout(NETWORK_CONNECTION_TIMEOUT, TimeUnit.MINUTES)
            .readTimeout(NETWORK_READ_TIMEOUT, TimeUnit.SECONDS)
            .writeTimeout(NETWORK_WRITE_TIMEOUT, TimeUnit.SECONDS)

        if (BuildConfig.DEBUG)
            okHttpClient.addInterceptor(provideHttpLoggingInterceptor())
        okHttpClient.addInterceptor(BasicAuthInterceptor(
                "superadmin@epaysave.com", "supadm_12345")).build();
        return okHttpClient.build()
    }

    fun provideHeaderInterceptor(headers: HashMap<String, String>): Interceptor {
        return Interceptor { chain ->
            val request = chain.request()?.newBuilder()
            headers.forEach {
                request?.addHeader(it.key, it.value)
            }
            chain.proceed(request!!.build())
        }

    }

    private fun provideHttpLoggingInterceptor(): HttpLoggingInterceptor {
        val logging = HttpLoggingInterceptor()
        logging.level = HttpLoggingInterceptor.Level.BODY
        return logging
    }
}

internal class BasicAuthInterceptor(userName: String, password: String) : Interceptor {

    private val credentials: String = Credentials.basic(userName, password)

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val authenticateRequest = request.newBuilder().addHeader("Content-Type", "application/json; charset=utf-8")
            .addHeader("Accept", "application/json").addHeader("Authorization", credentials).build()
        return chain.proceed(authenticateRequest)
    }
}

我在清除Android中的retrofit2和OKHTTP3中的kotlin应用程序时遇到问题。如何正确执行?

0 个答案:

没有答案