gpt4 book ai didi

java - 下载图像时 Blackberry Http 连接超时。为什么?

转载 作者:行者123 更新时间:2023-12-02 00:50:53 27 4
gpt4 key购买 nike

我的应用程序循环访问大约 200 个都是 jpg 图像的 URL。在模拟器中它读取正常,然后将字节数组存储在 persistenceStore 中,没有任何问题。在设备上,它会给出 java.io.IOException: TCP read timed out on 基本上每个图像。时不时就有一个人通过。甚至不知道如何。图像大小也无法提供洞察力。有的6k,有的11k。大小似乎对于超时并不重要。

我会尝试发布我认为相关的代码,但我并不是真正的专家,所以如果我遗漏了一些内容,请说出来。

通过loop和join线程调用http连接:

for(int i = 0; i < images.size(); i ++)
{
try {
String url = images.elementAt(i).toString();
HttpRequest data3 = new HttpRequest(url, "GET", false);
data3.start();

data3.join();

} catch (IOException e) {
Dialog.inform("wtf " + e);
}
}

在 HttpConnection 类中使用正确的后缀建立实际连接:

try
{
HttpConnection connection = (HttpConnection)Connector.open(url + updateConnectionSuffix());


int responseCode = connection.getResponseCode();
if(responseCode != HttpConnection.HTTP_OK)
{
connection.close();
return;
}

String contentType = connection.getHeaderField("Content-type");
long length = connection.getLength();

InputStream responseData = connection.openInputStream();
connection.close();

outputFinal(responseData, contentType, length);
}
catch(IOException ex)
{

} catch (SAXException ex) {

} catch (ParserConfigurationException ex) {

}

最后,读取流并将字节写入字节数组:

else if(contentType.equals("image/png") || contentType.equals("image/jpeg") || contentType.equals("image/gif"))
{
try
{
if((int) length < 1)
length = 15000;

byte[] responseData = new byte[(int) length];
int offset = 0;
int numRead = 0;
StringBuffer rawResponse = new StringBuffer();

int chunk = responseData.length-offset;
if(chunk < 1)
chunk = 1024;

while (offset < length && (numRead=result.read(responseData, offset, chunk)) >= 0){
rawResponse.append(new String(responseData, offset, numRead));
offset += numRead;
}

String resultString = rawResponse.toString();
byte[] dataArray = resultString.getBytes();

result.close();

database db = new database();
db.storeImage(venue_id, dataArray);
}
catch( Exception e )
{
System.out.println(">>>>>>>----------------> total image fail: " + e);
}


}

需要考虑的事情:
长度始终是模拟器中的字节长度。在设备中它始终为-1。
chunk var 是一个测试,看看我是否强制使用 15k 字节数组,它是否会尝试按预期读取,因为 byte[-1] 给出了越界异常。结果是一样的。有时会写。大多数情况下都会超时。

如有任何帮助,我们将不胜感激。

最佳答案

您可以使用参数“ConnectionTimeout”调整 Blackberry 上的 TCP 超时长度。

在您的代码中:

HttpConnection connection = (HttpConnection)Connector.open(url + updateConnectionSuffix());

您需要附加 ConnectionTimeout。您可以将其写入 updateConnectionSuffix() 或只是附加它。

    HttpConnection connection = (HttpConnection)Connector.open(url + updateConnectionSuffix() + ";ConnectionTimeout=54321");

这将超时设置为 54321 毫秒。

当客户端等待服务器发送确认但在指定时间内未收到确认时,就会发生超时。

编辑:另外,你能使用浏览器之类的东西吗?您可能还想使用设备端参数。

关于java - 下载图像时 Blackberry Http 连接超时。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3216998/

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