diff --git a/app/src/main/java/eu/m724/vastapp/vastai/cronet/StringUrlRequestCallback.kt b/app/src/main/java/eu/m724/vastapp/vastai/cronet/StringUrlRequestCallback.kt index a67c480..fb6dd37 100644 --- a/app/src/main/java/eu/m724/vastapp/vastai/cronet/StringUrlRequestCallback.kt +++ b/app/src/main/java/eu/m724/vastapp/vastai/cronet/StringUrlRequestCallback.kt @@ -25,7 +25,8 @@ open class StringUrlRequestCallback( } override fun onResponseStarted(request: UrlRequest?, info: UrlResponseInfo?) { - request?.read(ByteBuffer.allocateDirect(102400)) + val size = info?.allHeaders?.get("content-length")?.get(0)?.toIntOrNull() + request?.read(ByteBuffer.allocateDirect(size ?: 100000)) } override fun onReadCompleted( @@ -36,13 +37,19 @@ open class StringUrlRequestCallback( byteBuffer?.clear() request?.read(byteBuffer) - stringResponse.append(Charsets.UTF_8.newDecoder().onUnmappableCharacter(CodingErrorAction.IGNORE).decode(byteBuffer)) + stringResponse.append( + Charsets.UTF_8.newDecoder() + .onUnmappableCharacter(CodingErrorAction.IGNORE) + .decode(byteBuffer) + ) } override fun onSucceeded(request: UrlRequest?, info: UrlResponseInfo?) { if (info != null) { val body = stringResponse.toString() val statusCode = info.httpStatusCode + println(info.httpStatusCode.toString() + ": " + stringResponse.substring(0, stringResponse.length)) + println(info.allHeaders.toString()) if (statusCode == 200) { try { @@ -52,7 +59,7 @@ open class StringUrlRequestCallback( } } else if (statusCode >= 500) { onFailure(ServerError(statusCode, body)) - } else if (statusCode == 403) { + } else if (statusCode == 401) { onFailure(UnauthorizedException(body)) } else { onFailure(ClientException(body))