Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving incomplete response from retrofit

I am using retrofit version 2.6.1 for making http requests over the network. The JSON I am expecting is 42466 characters long. However, I am receiving only 4073 characters and API are working fine on web browser and postman.

So I added custom okhttp client and increase timeout but it does not help me.

private var okHttpClient: OkHttpClient = OkHttpClient().newBuilder()
    .connectTimeout(10, TimeUnit.SECONDS)
    .readTimeout(10, TimeUnit.SECONDS)
    .writeTimeout(10, TimeUnit.SECONDS)
    .build()

Then I tried adding a logging interceptor and I found that okhttp is receiving the response what I wanted in interceptor logs but in chunks.

private val httpInterceptor: HttpLoggingInterceptor = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
private var okHttpClient: OkHttpClient = OkHttpClient().newBuilder()
    .connectTimeout(10, TimeUnit.SECONDS)
    .readTimeout(10, TimeUnit.SECONDS)
    .writeTimeout(10, TimeUnit.SECONDS)
    .addInterceptor(httpInterceptor)
    .build()

At last, I assigned http client and interceptor to retrofit builder and this is how it's look

private val centralRetrofit = Retrofit.Builder().baseUrl("https://www.********.com/")
    .addConverterFactory(ScalarsConverterFactory.create())
    .client(okHttpClient)
    .build()
    .create(MusicAccess::class.java)

So, I think using post request will help me rather than get and tried taking all the response in string format to check the reponse

@Headers("Content-Type: text/html; charset=UTF-8")
@POST("**********")
fun getMusic(): Call<String>

But it also did not get to conclusion after that I thought http response will have size limit and used reader to access the json from url by following way.

val client = OkHttpClient()
val request = Request.Builder().url("********")
    .addHeader("Content-Type", "application/json")
    .build()
val response = client.newCall(request).execute()
val input = response.body()?.byteStream()
val reader = BufferedReader(InputStreamReader(input))

But as this https://stackoverflow.com/a/26023885/7639056 answer state that we cannot configure the OkHttpClient to read more than 2048 bytes from the buffer

So is there any way I can get all the data at once?

like image 237
Kaushik Burkule Avatar asked Oct 29 '19 20:10

Kaushik Burkule


1 Answers

Eventually I figured out that why this is happening. There were no problem with the HTTP request. But only a part of it was being printed in logcat due to it's limited buffer size.

There is a size limit of 1024 bytes for binary logs. The size limit for non-binary logs are as shown below.

#define LOGGER_ENTRY_MAX_LEN        (4*1024)
#define LOGGER_ENTRY_MAX_PAYLOAD (LOGGER_ENTRY_MAX_LEN - sizeof(struct logger_entry))

To set the limit to a larger number, Preferences/Settings -> Editor -> General -> Console, check the box next to Override console cycle buffer size, and enter the number.

I am feeling so embarrassed, but thank you for your support.

like image 112
Kaushik Burkule Avatar answered Nov 09 '22 03:11

Kaushik Burkule