BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 01-08-2009, 05:24 AM   #1
bindiamariam
New Member
 
Join Date: Dec 2007
Model: 7100T
PIN: N/A
Carrier: GPRS
Posts: 5
Default Application hangs on http connection on emulator

Please Login to Remove!

Hi All,

I am facing an issue in httpconnection on the black berry emulator. The application is able to make a connection and write to the output stream. After writing, it hangs at the flush(). Once I remove the writer.flush(), it hangs at the next statement i.e. where I try to get the resopnseCode. The server is receiving the request and sending the response back. Initially I had the J2ME code (with MIDlet), which works fine, now I am using RIM APIs in the application. I have started a new thread to connect to the server. Still the application is hanging. Any pointers will help.

Code:
public HTTPResponseVO sendMessage(HTTPRequestVO httpRequestVO) throws Exception {
        /* Response message from server */
        String responseMessage = "";
        /* Initialising a InputStream */
        InputStream inputStream = null;
        /* Initialising a OutputStream */
        OutputStream outputStream = null;
        /* Initialising a HttpConnection */
        HttpConnection httpConnection = null;
        int responseCode = 0;
        try {
            /*
             * Opening a connection to the server to the url specified in
             * httpRequest
             */
            httpConnection = (HttpConnection) Connector.open(httpRequestVO.url);
            if (httpConnectionMethodType.equals("POST")) {
                /* Setting the http connection method */
                httpConnection.setRequestMethod("POST");
            } else {
                /* Setting the http connection method */
                httpConnection.setRequestMethod("GET");
            }
            /* Setting the header properties for the connection */
            httpConnection.setRequestProperty("If-Modified-Since", ifModifiedSince);
            httpConnection.setRequestProperty("Connection", connection);
            httpConnection.setRequestProperty("Proxy-Connection", proxyConnection);
            httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            httpConnection.setRequestProperty("User-Agent", "BlackBerry/3.2.1");
            httpConnection.setRequestProperty("Content-Language", contentLanguage);
        } catch (Exception exception) {
            throw new Exception("Error in connection");
        }
        if (httpConnection != null) {
            try {
                /* Opening an output stream */
                outputStream = httpConnection.openOutputStream();
                if (outputStream != null) {
                    OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
                    writer.write(httpRequestVO.message);
                     //writer.flush();
                     writer.close();
                     outputStream.close();
                } else {
                    throw new Exception("outputStream is null");
                }
                System.out.println("waiting for responseCode :");
                /* Getting the response code of the connection */
                responseCode = httpConnection.getResponseCode();
                System.out.println("responseCode :" + responseCode);
                /* If response code is not OK throw SSEInternalException */
                if (responseCode != HttpConnection.HTTP_OK) {
                    throw new Exception("HttpConnection. not OK");
                }
                inputStream = httpConnection.openInputStream();
                if (inputStream != null) {
                    InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
                    /*
                     * Getting the number of bytes that can be read from the
                     * input stream
                     */
                    int readCharacter;
                    /* Creating a new byte array of size totalLength */
                    StringBuffer responseBuffer = new StringBuffer();
                    while ((readCharacter = reader.read()) != -1) {
                        responseBuffer.append((char) readCharacter);
                    }
                    /* Converting the received bytes to string */
                    responseMessage = new String(responseBuffer);
                } else {
                    throw new Exception("inputStream null");
                }
            } catch (IOException io) {
                throw new Exception(io.getMessage());
            } catch (Exception ex) {
                throw new Exception(ex.getMessage());
            } finally {
                try {
                    if (outputStream != null) {
                        /* Closing the output stream */
                        outputStream.close();
                    }
                    if (inputStream != null) {
                        /* Closing the input stream */
                        inputStream.close();
                    }
                    if (httpConnection != null) {
                        /* Closing the http connection */
                        httpConnection.close();
                    }
                } catch (Exception ex) {
                    throw new Exception(ex.getMessage());
                }
            }
        }
        /*
         * Return a new instance of HTTPResponseVO with the response   message and
         * response code
         */
        return new HTTPResponseVO(responseMessage, responseCode);
    }
Thanks
Bindia Baby
Offline  
Old 01-08-2009, 08:24 AM   #2
Dougsg38p
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Default

HTTPConnection.getHTTPResponseCode() actually forces the flush.

Do you have MDS running? What connection parameter string are you using? Did you try setting a Timeout parameter? It could be that the connection is simply failing.
Offline  
Old 01-09-2009, 01:49 AM   #3
bindiamariam
New Member
 
Join Date: Dec 2007
Model: 7100T
PIN: N/A
Carrier: GPRS
Posts: 5
Default

I had my MDS running when the connection was failing. The server was sending the response but the client was not able to read it. Now I resolved the issue on emulator by giving the parameter deviceside=true in the URL. Now I have a doubt, should I be using the same parameter on the device or should I use the default, deviceside=false. As I understand if I give deviceside=true the MDS will be bypassed to make the connection, but when I was using a MIDlet the normal connection was working with MDS but when I created a UIApplication it started failing, Any pointers?
Offline  
Old 01-09-2009, 08:17 AM   #4
Dougsg38p
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Default

With MDS/BES, you should not be setting any "deviceside" parameter.

If the server is responding but the client is not receiving the response, you might have an "Accept" type issue. If you set a particular "Accept" header (like text/xml), and the response comes back with a different content type, MDS will eat the response and return a 406 error.

Try turning on verbose logging in your MDS simulator. This gives you the actualy details between MDS and the client.
Offline  
Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


MSA ALTAIR 4X Multigas Monitor Detector - 10110030 &ALTAIR Pump probe 10152669 picture

MSA ALTAIR 4X Multigas Monitor Detector - 10110030 &ALTAIR Pump probe 10152669

$395.50



MSA ALTAIR 4XR 10178557 Multigas Detector, (LEL, O2, H2S & CO), Charcoal case picture

MSA ALTAIR 4XR 10178557 Multigas Detector, (LEL, O2, H2S & CO), Charcoal case

$699.00



MSA Altair 5X Multi Gas Detector - COMB, O2, CO, H2S,CO2。Monochrome screen picture

MSA Altair 5X Multi Gas Detector - COMB, O2, CO, H2S,CO2。Monochrome screen

$500.00



MSA ALTAIR 4XR picture

MSA ALTAIR 4XR

$700.00



MSA 10042621 Altair 5X Sampling Probe Straight Air-Line 1' Color Black (E2) picture

MSA 10042621 Altair 5X Sampling Probe Straight Air-Line 1' Color Black (E2)

$295.00



Altair WBS-202 Dual channel wireless intercom base station picture

Altair WBS-202 Dual channel wireless intercom base station

$1350.00







Copyright © 2004-2016 BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of BlackBerry Inc.