gpt4 book ai didi

java - 安卓 java.lang.IllegalArgumentException : byteCount

转载 作者:行者123 更新时间:2023-12-05 07:47:46 46 4
gpt4 key购买 nike

我从这段代码中得到随机错误(不是所有连接)java.lang.IllegalArgumentException: byteCount < 0: -291674

URL connurl = new URL(newUrl);
conn = (HttpURLConnection) connurl.openConnection();
conn.setRequestProperty("User-Agent", "Android");
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (finish != 0L)
conn.setRequestProperty("Range", "bytes=" + finish + "-" + size);
conn.connect();
int getResponseCode = conn.getResponseCode();
String getContenttype = conn.getContentType();
int contentLenght = conn.getContentLength();
if (!StringUtils.startsWith(String.valueOf(getResponseCode), "20")) {
}
else if(StringUtils.startsWithIgnoreCase(getContenttype, "text/html")) {
}
else if(contentLenght < (size-finish)) {
}
else {
ReadableByteChannel channel = Channels.newChannel(conn.getInputStream());
accessFile = new RandomAccessFile(filePath, "rwd");
accessFile.seek(finish);
fileStream = new FileOutputStream(filePath, true);
ByteBuffer buffer = ByteBuffer.allocate(1024);
byte[] data;
int temp;
while ((temp = channel.read(buffer)) > 0) { <-- error in this line
}

错误:

java.lang.IllegalArgumentException: byteCount < 0: -1647333
at com.android.okhttp.okio.RealBufferedSource.read(RealBufferedSource.java:46)
at com.android.okhttp.internal.http.HttpConnection$FixedLengthSource.read(HttpConnection.java:418)
at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:349)
at java.io.InputStream.read(InputStream.java:162)
at java.nio.channels.Channels$InputStreamChannel.read(Channels.java:306)

如何解决?谢谢

最佳答案

这可能与该问题有关:https://github.com/square/okhttp/issues/3104

按照建议,考虑使用 okhttp-urlconnection,例如:

OkHttpClient okHttpClient = new OkHttpClient();
URL.setURLStreamHandlerFactory(new OkUrlFactory(okHttpClient));

关于java - 安卓 java.lang.IllegalArgumentException : byteCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39303278/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com