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
Posts: 160
Registered: ‎03-02-2011
My Device: Not Specified

HTTP Connection PROBLEM

Hello, I am trying to send an image to the server using the HTTP connection. What I am doing is the following:

1- Read the image Bytes

2- Convert the image to a Base64 String

3- Send the String to the server

 

The problem is that when sending a big base64 String to the server, this doesn't work!!!

I followed the code and the problem appears on the outputStream.flush() method. I don't know what's wrong, but as a solution I added 8 lines of outputStream.flush(), this works!! But i need a good solution because when the image size augments, i need to flush again more than 8 times. The code of the connection is as follows:

 

private OutputStream os = null;

os = _httpConnection.openOutputStream();

 

os.write(postDataBytes);
//When i add a lot of os.flush lines, the code will work
os.flush();
Anyone can help please?

 

Regular Contributor
Posts: 64
Registered: ‎01-27-2011
My Device: Not Specified

Re: HTTP Connection PROBLEM

Are you immediately calling the close() method after the flush(). I got an error when I did that, my workaround was to comment out the flush method and call close() after I do a write().

 

Hope this helps!

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: HTTP Connection PROBLEM

These both seem like work arounds, is there an OS problem here?

 

Can you tell us what size the images are and what connection method you are using?  Also what OS levels are involved (for compilation and device/simulator).

New Developer
Posts: 160
Registered: ‎03-02-2011
My Device: Not Specified

Re: HTTP Connection PROBLEM

Hello peter,

 

The image size is about 30 Kb and the OS involved is 5.0 (the simulator OS). But I want that to work also on 4.6 devices.

The class used to connect is the following:

 

 

    public UiHttpConn(String url, String data) {
        //is = new InputStream();
        _postData = data;
        _url = url;
        _data = new byte[100];
        try {
        _httpConnection = (HttpConnection)Connector.open(_url);
        } catch(Exception e) {
        }
    }
    public String postData() {
        StringBuffer sb = null;
         try {
            byte [] postDataBytes = _postData.getBytes();
            _httpConnection.setRequestMethod(HttpConnection.POST);
            _httpConnection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
            _httpConnection.setRequestProperty("Content-Language", "en-US");
            _httpConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            _httpConnection.setRequestProperty("Content-Length",new Integer(postDataBytes.length).toString());
            os = _httpConnection.openOutputStream();
            os.write(postDataBytes);
            
            os.flush();
            os.close();
            
            int rc = _httpConnection.getResponseCode();
            if(rc == HttpConnection.HTTP_OK) {
                is = _httpConnection.openInputStream();
                if (null != is) {
                    sb = new StringBuffer();
                    int read;
                    try {
                        while ((read = is.read()) >= 0) {
                            sb.append( (char)read );
                        }
                        is.close();
                    }
                    catch (Exception ex) { }
                }
            } else {
                //Dialog.alert("No Connection Available");
            }
       } catch(Exception e) {
       }
       return (sb.toString());
       //return (new String(_data));
    }
So how to solve that problem? I guess it is a problem with the flush method

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: HTTP Connection PROBLEM

First a couple of minor comments

 

1) the following code creates an Integer object that is then immediately discarded:

new Integer(postDataBytes.length).toString()

 

instead do this:

Integer.toString(postDataBytes.length)

 

2) If you want this code to work in 4.6, you should be compiling and running in 4.6.  OS 5.0 code is not supported on OS 4.6.  OS 4.6 code is supported on OS 5.0.  However there are issues of APIs that you might want to use not available in OS 4.6.  For me these are mostly around the detection of touch events.  SO I end up doing a pre OS 4.7 build and an OS 4.7 and above build (at least).

 

Now onto the problem.  You code looks fine, the size of file should be able to be sent.  There is an issue with the encoding:

_httpConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

 

URL encoding will replace + with spaces, or is it the other way round?  Can't remember.  Result is that this corrupts Base 64 encoded data.  Talk to your Server people about using another encoding method. 

 

However this does not explain your problem, at least I don't think it does.  This problem would cause corruption in the image sent. 

 

Can you change your Server processing so that it just logs the posted data and does not try to process it.,  This should work regardless of the encoding.  And if it does work, then you have a Server issue.

 

But this is only a guess.  From my review, your code looks fine. 

 

Actually there is potential problem in your processing of the response.  Another encoding issue.  Your code will assume ISO-8859-1 encoding in the response.  But we can look at this another time.