收到改造的不完全答复 [英] Receiving incomplete response from retrofit
问题描述
我正在使用改进版本2.6.1
来通过网络发出http请求.我期望的JSON长42466个字符.但是,我只收到4073个字符,并且API在Web浏览器和邮递员上工作正常.
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.
因此,我添加了自定义okhttp客户端并增加了超时时间,但这对我没有帮助.
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()
然后,我尝试添加一个日志记录拦截器,发现okhttp在拦截器日志中收到了我想要的响应,但以块的形式收到了.
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()
最后,我将http客户端和拦截器分配给了改造构建器,这就是它的外观
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>
但是在我认为http响应将具有大小限制并使用读取器通过以下方式从url访问json时,它也没有得出结论.
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))
但是由于 https://stackoverflow.com/a/26023885/7639056 的回答状态,我们无法配置OkHttpClient从缓冲区读取超过2048个字节
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?
推荐答案
最终,我弄清楚了为什么会这样. HTTP请求没有问题.但由于缓冲区大小有限,因此只有一部分被打印在logcat中.
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.
二进制日志的大小限制为1024字节.非二进制日志的大小限制如下所示.
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))
要将限制设置为更大的数字,请Preferences/Settings -> Editor -> General -> Console,
选中覆盖控制台循环缓冲区大小"旁边的框,然后输入数字.
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.
这篇关于收到改造的不完全答复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!