Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
telematicus
Posts: 9
Registered: ‎06-29-2011
My Carrier: Not specified

Connection close when send a big file

Hi everyone.

 

I'm developing an application in the BB Bold 9780 Simulator.

The application opens several connections (HttpConnection) with our server to get information and upload files (ZIP, XML and JPG) to the server.

 

With standard connection (short HTTP connections) we have no problem at all, but when we try to upload a file always happens the same exception "net.rim.device.api.io.ConnectionClosedException: Stream closed".

 

We've just  discarded a problem with the server or proxy as long as an android application (within the same environment) works with no problem.

 

NOTE: Whatever the file we try to send the server only receive a 1232 bytes length array (a little more than 1KB) so:

  • Is there any limit size for sending data throw a HttpConnection in BB?
  • Did anybody have the same problem?
  • Any suggested workaround?

Thanks in advance

 

Following is the code we are using to send the files:

(Notice I have changed the code so do not surprise if you find something weird)

 

protected String requesting(String method, Contrary parameters) throws Exception {
        HttpConnection c = null;
        OutputStream os = null;
        FileConnection f = null;
        InputStream is=null;
        int rc;
        try {
            //Gets the filename
            String filename=parameters.get(0).toString();
            //Creates the file
            f=(FileConnection)Connector.open(filename,Connector.READ);
            if(!f.exists()) {
                throw new FileNotFoundException(filename);
            }
            c = openHttpConnection();//http://server:smileytongue:ort/resource.aspx;ConnectionTimeout=5000;interface=wifi

            int pos=filename.lastIndexOf('/');
            if(pos>-1) {
                filename=filename.substring(pos+1);
            }
           
            // Set the request method and headers
            c.setRequestMethod(HttpConnection.POST);
            c.setRequestProperty("Content-Length",String.valueOf(f.fileSize()));
            c.setRequestProperty("Keep-Alive","timeout=60000, max=60000");
            c.setRequestProperty("Connection","Keep-Alive");
            c.setRequestProperty("Content-Type",parameters.get(1).toString());
           
            // Getting the output stream may flush the headers
            os=c.openOutputStream();
            is=f.openInputStream();
            int readBytes = 0;
            byte[] buf = new byte[512];
            while ((readBytes = is.read(buf)) != -1) {
                os.write(buf,0,readBytes);
            }
           
            os.flush();
           
            // Getting the response code will open the connection,
            // send the request, and read the HTTP response headers.
            // The headers are stored until requested.
            rc = c.getResponseCode();
            if (rc != HttpConnection.HTTP_OK) {
                throw new JsonServiceException(String.valueOf(rc) + Util.STRING_TWO_POINTS + c.getResponseMessage());
            }
           
            return Util.STRING_TRUE;
        } catch (Exception e) {
            throw new JsonServiceException("Not an HTTP URL: "+e.getMessage());
        } finally {
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    //Do nothing
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    //Do nothing
                }
            }
            if (c != null) {
                try {
                    c.close();
                } catch (IOException e) {
                    //Do nothing
                }
            }
            if(f != null) {
                try {
                    f.close();
                } catch (IOException e) {
                    //Do nothing
                }
            }
        }
    }




Please use plain text.
Developer
ankityadav4u
Posts: 231
Registered: ‎03-16-2011
My Carrier: AIRTEL, BSNL

Re: Connection close when send a big file

not sure but u can try increasing connection timeout limit


available on: Monday to Friday, Indian standard (day) time : )
  • Give kudos by clicking on the star icon to say "Thanks!" if you feel this response is helpful.
  • Click "Accept as Solution" if this post was what you needed. so that people who are trying to help will not waste their time reading a thread where no help is needed.

Please use plain text.
Developer
peter_strange
Posts: 19,515
Registered: ‎07-14-2008

Re: Connection close when send a big file

Code looks OK, couple of minor things:

a)  I normally set a content length on a post. 

b) I would close the outputstream after the flush.

 

Don't think either of these will change what you see.

 

Can you accumulate and confirm the total number of bytes actually written in this loop?

            while ((readBytes = is.read(buf)) != -1) {
                os.write(buf,0,readBytes);
            }

Can you tell us what the content type actually is?

 

Can you tell us how big the file actually is in total?

 

Regarding your questions:

 

"Is there any limit size for sending data throw a HttpConnection in BB?"

This varies by connection method.  In your case you seem to be using WiFi, in which case as far as i know there is no limit.


"Did anybody have the same problem?"

Not me, but I have always sent large files in 64 KB chunks and had some Server code manage the concatenation of this.  I generally also Base64 encode them to make sure the data is actually text, but I don't think that is necessary. 

Please use plain text.
New Developer
telematicus
Posts: 9
Registered: ‎06-29-2011
My Carrier: Not specified

Re: Connection close when send a big file

Thanks for the reply.
I 've just proved with infinite time, 10 minutes or even 1 hour to be sure.
Please use plain text.
New Developer
telematicus
Posts: 9
Registered: ‎06-29-2011
My Carrier: Not specified

Re: Connection close when send a big file

Hi peter_strange

 

Thanks for your reply.

 

I answer your questions:

 

<<Can you accumulate and confirm the total number of bytes actually written in this loop?

            while ((readBytes = is.read(buf)) != -1) {
                os.write(buf,0,readBytes);
            }

 

Yes the accumulative readBytes matches with the file size


<<Can you tell us what the content type actually is?

 

I'll try with the following:

JPEG

XML

ZIP (really a XML compressed)

 

<<Can you tell us how big the file actually is in total?

XML -  142979 bytes

ZIP - 3706 bytes

JPEG (small) - 50,193 bytes

JPEG (big) -  1,128,999 bytes

 

I've also tried with with and without Base 64 encoded.

 

I've notice that with GPRS connection is working fine, but not with the WIFI Connection

 

Any ideas?

Please use plain text.
Developer
peter_strange
Posts: 19,515
Registered: ‎07-14-2008

Re: Connection close when send a big file

I must admit I don't understand this, WiFi should be the easiest of the options to get going. 

 

Allied to my comments previously, I would not send 1 MB data in a single chunk especially over GPRS.  it ill take some time and is quite likely to suffer from some sort of drop out and then you will need to send it all again.  But that is just me. 

 

I can't understand why you server will only get just over 1 KB when you send more over WiFi.  But my immediate reaction is that this is not a BB problem.  It is something else in the network.  So I would try to isolate where that might be. 

Please use plain text.
New Developer
telematicus
Posts: 9
Registered: ‎06-29-2011
My Carrier: Not specified

Re: Connection close when send a big file

Anything else.

I've been using the android emulator from the same PC to be sure anything the network is OK. But only happens with the BB WIFI connection

Please use plain text.