неожиданный конец потока в Retrofit-2.0 - PullRequest
1 голос
/ 17 июня 2020

Я пытаюсь отправить некоторые данные в свой API usind Retrofit2, но при первой попытке я получаю сообщение об ошибке «Тайм-аут», а иногда и «Неожиданный конец потока». Какие могут быть причины? Я использую приведенный ниже код:

Interface : 
interface APIService  {
@Headers("Authorization: ***************")
@POST("corona-watch-api/v1/reports/suspected-cases")
fun uploadImageToApi(@Body suspect_cas :suspected  ): Call<suspected?>?}

В основном действии я использую эту функцию для отправки данных в свой API:

  fun PostDataToApi(){

    var user : ArrayList<Int> = ArrayList()
    user.add(98)
    var attach = Attachment("3","ff","hh","hh","rrr")
    var cas_suspect =  suspected (attach,"hhh",false,"jjj",user)

    val loggingInterceptor = HttpLoggingInterceptor()
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)

    val okHttpClient = OkHttpClient().newBuilder()
        .retryOnConnectionFailure(true)
        .connectTimeout(60, TimeUnit.SECONDS)
        .readTimeout(60, TimeUnit.SECONDS)
        .writeTimeout(60, TimeUnit.SECONDS)

    okHttpClient.addInterceptor(loggingInterceptor).build()
    okHttpClient.retryOnConnectionFailure(true)

    okHttpClient.addInterceptor { chain ->
        val request = chain.request().newBuilder()
            .header("Accept", "application/json")
            .addHeader("Connection", "close")
            .build()
        chain.proceed(request)
    }

    val BaseUrl = "http://corona-watch-api.herokuapp.com/"
    val retrofit = Retrofit.Builder()
        .baseUrl(BaseUrl)
        .client(okHttpClient.build())
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    val service = retrofit.create(APIService::class.java)
    val call = service.uploadImageToApi(cas_suspect)


    call!!.enqueue(object : Callback<suspected?> {
        override fun onFailure(call: Call<suspected?>, t: Throwable) {
            Log.e("log", "Unable to submit post to API." + t.message)
        }

        override fun onResponse(call: Call<suspected?>, response: Response<suspected?>) {
            if (response.isSuccessful()) {
                println(response.body()!!.toString())
                Log.i( "log","post submitted to API." + response.body()!!.toString())
            }
        }
    })
}
...