Hi All,
When I post an image to server and then try to get response(XML-file) from input stream I catch net.rim.device.api.io.ConnectionClosedException. I open connection in separate thread (not in Event Dispatcher thread). Also I use JDE 4.3.0 and try to run my app on Pearl(8100).
Here is my code:
Code:
HttpConnection httpConnection = null;
OutputStream outputStream = null;
InputStream inputStream = null;
try {
httpConnection = (HttpConnection)Connector.open(url/* + ";deviceside=true"*/, Connector.READ_WRITE, true);
httpConnection.setRequestProperty("Content-Type", "application/atom+xml; charset=utf-8");
httpConnection.setRequestProperty("User-Agent", "MyApp 1.0 for BlackBerry");
String xwsse = auth(userName, password);
httpConnection.setRequestProperty("X-WSSE", xwsse);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
/*
here I generate and write XML-file(that content Base64 encoded image) to the baos.
*/
outputStream = httpConnection.openOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(outputStream, "UTF-8");
String output = baos.toString();
if(output != null)
{
osw.write(output, 0, output.length());
}
int responseCode = httpConnection.getResponseCode();
/*
on device and on emulator response code = 201. Also image resource is created on the server and I can see it there.
*/
StringBuffer sb = new StringBuffer();
inputStream = httpConnection.openInputStream();
InputStreamReader isReader = new InputStreamReader(inputStream);
char i;
int j = 0;
while( (j = isReader.read()) != -1)
{
i = (char)j;
sb.append(i);
}
/*
then I try to parse sb that consists response XML, and get server image's href
*/
}catch(Throwable t)
{
}
finally{
if(outputStream != null)
{
outputStream.close();
outputStream = null;
}
if(inputStream != null)
{
inputStream.close();
inputStream = null;
}
if(httpConnection!= null)
{
httpConnection.close();
httpConnection= null;
}
}
This code work properly on emulator, but when I try to read response from httpConnection on real device it throws ConnectionClosedException.
Please help me!!!
Regards, Pasha.