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
Contributor
Posts: 48
Registered: ‎07-10-2010
My Device: 8300

Issue in making a Post Request using wifi

Hi,

I'm making a post request to my web services using the http coonection.

The application works fine and successfully makes a post request on simulator.I make use of the below code to make HTTP POST request

 

 

  public String sendPostRequest(String defaultURL,String requeststring) {
                HttpConnection hc = null;
                DataInputStream dis = null;
                DataOutputStream dos = null;
                
                StringBuffer messagebuffer = new StringBuffer();
                try {
                        // Open up a http connection with the Web server
                        // for both send and receive operations
                        hc = (HttpConnection)
                        Connector.open(defaultURL, Connector.READ_WRITE);
                        // Set the request method to POST
                        hc.setRequestMethod(HttpConnection.POST);
                        // Send the string entered by user byte by byte
                        dos = hc.openDataOutputStream();
                        byte[] request_body = requeststring.getBytes();
                        for (int i = 0;i< request_body.length; i++) {
                                dos.writeByte(request_body[i]);
                        }
                        dos.flush();
                        dos.close();
                        // Retrieve the response back from the servlet
                        dis = new DataInputStream(hc.openInputStream());
                        
                        System.out.println("Save request Result : " + dis);
                        int ch;
                        // Check the Content-Length first
                        long len = hc.getLength(); 
                        if(len!=-1) {
                                for(int i = 0;i<len;i++)
                                        if((ch = dis.read())!= -1)
                                                messagebuffer.append((char)ch);
                                        } else {
                                                // if the content-length is not available
                                                while ((ch = dis.read()) != -1)
                                                        messagebuffer.append((char) ch);
                                        }
                        dis.close();
                        
                        
                } catch (IOException ioe) {
                        messagebuffer = new StringBuffer("ERROR!");
                } finally {
                        // Free up i/o streams and http connection
                        try { 
                                if (hc != null) hc.close();
                        } catch (IOException ignored) {}
                        try { 
                                if (dis != null) dis.close();
                        } catch (IOException ignored) {}
                        try { 
                                if (dos != null) dos.close();
                        } catch (IOException ignored) {}
                }
                return messagebuffer.toString();
        }

 

 And i call this method  as   below

 

HttpPost _objHttpPost = new HttpPost();
String _resultString = _objHttpPost.sendPostRequest(	"bla....bla.../service/WSWakeupService.asmx/SaveWakeupRequest",           _requestString);

 

This works fine on simulator

 

 

But now when testing the application on device, i make use of the wifi network so  i call the same function as

HttpPost _objHttpPost = new HttpPost();
String _resultString = _objHttpPost.sendPostRequest(
"bla.....bla..../service/WSWakeupService.asmx/SaveWakeupRequest;deviceside=true;interface=wifi",_requestString);

  however this doesnt work and my request is not saved on the server side

please could you help me in this

 

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

Re: Issue in making a Post Request using wifi

The connection suffix you are adding seems correct to me, though you do not actually need the ";deviceside=true".

 

Can you tell us exactly what happens at the BlackBerry end.  It should fail with an exception if it doesn't give you a good response.  What exception do you see?

Contributor
Posts: 48
Registered: ‎07-10-2010
My Device: 8300

Re: Issue in making a Post Request using wifi

Hi thanks for the reply.

i dont see any error or exception.

Actually the server component is not under my control , i can only view a web site of our server component and only see if  the request is successfully saved.

 

But if i have to test the same on simulator ,it works fine and my request gets saved on the server component.

 

 

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

Re: Issue in making a Post Request using wifi

"I dont see any error or exception."

 

Unfortunately that does not mean it did not happen!  In my experience, something always happens at the BlackBerry end.  Probably the easiest way to find it is to debug on the device and step through your code, especially sendPostRequest.  You should put break points on the Exception processing too to see if that gets called. 

 

Let us know how you get on with this.