IOException:未连接类:blackberry中的类net.rim.device.api.io.ConnectionClosedException
我从Blackberry的java代码中获得net.rim.device.api.io.ConnectionClosedException。下面代码中突出显示的是引发异常的行IOException:未连接类:blackberry中的类net.rim.device.api.io.ConnectionClosedException,exception,blackberry,connection,Exception,Blackberry,Connection,我从Blackberry的java代码中获得net.rim.device.api.io.ConnectionClosedException。下面代码中突出显示的是引发异常的行 hc = (HttpConnection) Connector.open(url); hc.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + getBoundaryString()); hc.s
hc = (HttpConnection) Connector.open(url);
hc.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + getBoundaryString());
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty(HttpProtocolConstants.HEADER_ACCEPT_CHARSET,
"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
hc.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH ,Integer.toString(fileBytes.length));
OutputStream dout = hc.openOutputStream();
dout.write(boundaryMessage.getBytes());
dout.write(this.postBytes);
dout.write(endBoundary.getBytes());
dout.close();
int ch;
**is = hc.openInputStream();** //exception raised here
if(hc.getResponseCode()== HttpConnection.HTTP_OK)
{
while ((ch = is.read()) != -1)
{
bos.write(ch);
}
res = bos.toByteArray();
System.out.println("res loaded..");
}
else {
System.out.println("Unexpected response code: " + hc.getResponseCode());
hc.close();
return null;
}
}
catch(IOException e)
{
System.out.println("====IOException : "+e.getMessage()+" Class: "+e.getClass());
}
catch(Exception e1)
{
//e1.printStackTrace();
System.out.println("====Exception : "+e1.getMessage()+" Class: "+e1.getClass());
}
finally
{
try
{
if(bos != null)
bos.close();
if(is != null)
is.close();
if(hc != null)
hc.close();
}
catch(Exception e2)
{
e2.printStackTrace();
System.out.println("====Exception : "+e2.getMessage());
}
}
我应该怎么做来纠正这个问题?有人能帮忙吗?提前感谢。不要调用
dout.close()代码>在完成此连接的所有操作之前,不要关闭任何连接。如果要刷新流,请调用dout.flush()
,但不要关闭它。dout.close()不关闭连接,只关闭输出流。flush()调用可能很有用。另一件要尝试的事情(我将它与我的代码进行比较)是在打开输入流之前获取HTTP响应代码。如果它不起作用,那么它可能取决于其他因素。我看到您正在使用一个多部分请求,您确定边界字符串都正常吗?感谢G B的响应。是的,我的边界字符串还可以。告诉你,我感到恼火的是,当我不得不上传其他音频/文本文件时,我已经运行了好几次这段代码。所以这段代码应该也能工作,但它在这里不起作用。我唯一更改的是dout.write(boundaryMessage.getBytes());dout.write(this.postBytes);write(endBoundary.getBytes());当我以前只有一个dout.write(postbytes)时,我必须写入的整个字节块在哪里。由于ByteArrayOutputStream内存问题,我不得不编辑代码。这可能是问题所在..在dout上写了3次?我试图将hc.OpenInputStream()放在hc.getResponseCode()之后,但仍然不起作用。你能再帮我一次吗?