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. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Web and WebWorks Development

Reply
New Contributor
GautamPowar
Posts: 6
Registered: ‎01-25-2011
My Carrier: Blackberry Application Developer

How to make Http connection in blackberry webworks

Hi All,

             I'm developing a sample application which needs to establish Http connection  with server running on my local machine. The requirement is to send the request to server as a JSON Object.            

 

I tried following code for this :-

-------------------------------------------- socket.html ---------------------------------------------------------

 

<html>
      <head>
              <meta name="viewport" id="viewport" content="initial-scale=1.0,user-scalable=no" />

              <script type="text/javascript">

 

                  var xmlHttp;
              
                  if (window.XMLHttpRequest)
                  {
                      // code for IE7+, Firefox, Chrome, Opera, Safari
                      xmlHttp =  new XMLHttpRequest();
                  }
                  else(window.ActiveXObject)
                  {
                      // code for IE6, IE5
                      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                  }
                  
                        
                  function connect_socket()
                  {
                        alert("connect_socket....");
                       
                        var url = "http://127.0.0.1:8070/?20:{\"command\":\"status\"}";


                        alert("opening connection");
                        xmlHttp.open('GET', url, true);
                        xmlHttp.onreadystatechange = callbackFunction;
                        xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                    
                        xmlHttp.send(null);
                  }
                        
                        
                  function callbackFunction()
                  {
                       alert("callbackFunction");
                       
                       if (xmlHttp.readyState != 4)
                          return;
                
                        var result = xmlHttp.responseText;
                        alert("Result is >>>> " + result);

 

                 }

 

               </script>
      </head>
      
      <body>
                <button onClick="connect_socket()">Get Single Object</button>
                <div id="results"></div>
      </body>
      
</html>

 

----------------------------------------------------------------------------------------------------------------------------------

 

The command which i need to send to server is :-  20:{"command":"status"}

where "20" is length of  json object.  I send it as shown in bold  above.

 

I'm not sure whether the request is getting send successfully to server.

 

Can i do the same thing using socket connection in webworks?

 

If so, please provide useful links...!!!

 

 

 

 

 

Please use plain text.
BlackBerry Development Advisor
tneil
Posts: 3,693
Registered: ‎10-16-2008
My Carrier: Rogers

Re: How to make Http connection in blackberry webworks

Hi GautamPowar,

 

There isn't WebSockets support in WebWorks or the Browser yet.  Have you made sure to whitelist your external domain in the config.xml?

 

<access uri="http://127.0.0.1:8070" subdomains="true" />

 

In order to perform a cross origin ajax call you need to whitelist the origin.

Tim Neil
Director, Application Platform & Tools Product Management
Follow me on Twitter
Please use plain text.