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

Web and WebWorks Development

Reply
New Developer
Posts: 16
Registered: ‎12-11-2009
My Device: 9500
My Carrier: IT

How to Consume Dot Net Webservice in Blackberry Widget ?

Hi,

I want to consume the Dot Net Web service in the Blackberry widget.. Im using the following code 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

 

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<script language="JavaScript">

var iCallID;

 

function init()

{

 

try

{

alert("before service call");

ser1.useService("http://localhost:1554/MyWebService.asmx?WSDL","servicename");

alert("after service call");

}

catch(e)

{

alert(e.description);

}

}

 

 

 

function onmyresult()

{

alert("in result");

   if ((event.result.error)&&(iCallID==event.result.id))

   {

      var xfaultcode = event.result.errorDetail.code;

      var xfaultstring = event.result.errorDetail.string;

      var xfaultsoap = event.result.errorDetail.raw;

 

      // Add code to output error information here

      alert("Error ");

   }

   else

   {

  ser1.innerHTML= "The method returned the result: "

                         + event.result.value;

   }

}

</script>

</HEAD>

<body onload="init();">

<BR>

Enter a Value

<input type='text' id='param1'>

<BR>

<button onclick='iCallID = ser1.servicename.callService(HelloWorld)'>Call

A Web Method</button>

<div id="ser1" style="behavior: url(webservice.htc)"

onresult="onmyresult();"></div>

</body>

</html>



But im getting an error message at "Web service Url"..And im getting an error message that"Undefined". 
Im new to BB widget and Can anybody help me solving this problem.
Im using Blackberry widget perspective in Eclipse.
 

 

Highlighted
Developer
Posts: 25
Registered: ‎07-24-2008
My Device: Not Specified

Re: How to Consume Dot Net Webservice in Blackberry Widget ?

Hi

 

In a Widget application you have to add it to your white list in the config.xml file like:

 

  <access uri="http://IPAddress/Service.asmx" subdomains="true" />

 

 

I find IP addresses are the most reliable way to do this. I've seen on this forum that Lusing just localhost/Service.asmx can sometimes cause problems

 

Not sure about your JavaScript as there are many different ways of calling the service depending on what you want to achieve.

 

Hope this helps

 

Davy

New Developer
Posts: 16
Registered: ‎12-11-2009
My Device: 9500
My Carrier: IT

Re: How to Consume Dot Net Webservice in Blackberry Widget ?

Hi Davy,

We even tired this option of adding IP domain to access permission.

here is my code and error which I am getting.

 


 

 

Code:

 

<html>

<head>

<title>Hello World</title>

<script language="JavaScript" type="text/javascript">

var iCallID;

function InitializeService()

{

service.useService("webservice addressl", "service2");

service.service2.callService("gimmePower");

}


function ShowResult()

{

document.getElementById("lblResult").innerHTML = event.result.value;

var result=event.result.value

}

</script>

</head>

<body onload="InitializeService()" id="service" style="behavior:url(webservice.htc)" onresult="ShowResult()">

<label id="lblResult">Result</label>

</body>

</html>

 

 


 

Error:

 

TypeError: undefined is not a function.

Running at line 5: service.useService("webservice address","service2");.

Called from line 1: InitializeService()} } } }.

 

 

New Developer
New Developer
Posts: 17
Registered: ‎11-26-2009
My Device: Bold 9700
My Carrier: T-Mobile GER

Re: How to Consume Dot Net Webservice in Blackberry Widget ?

have you added the uri? like "http://123.345.567.89/Widget/Service1.asmx?WSDL" ?

 

But by the way, i'm gettin the same error. In IE, everything works fine, but on the BB the javascript cannot handle the useService method.

New Developer
Posts: 16
Registered: ‎12-11-2009
My Device: 9500
My Carrier: IT

Re: How to Consume Dot Net Webservice in Blackberry Widget ?

Hi , 

thanks for your response.I added the uri like   http://123.345.567.89/Widget/Service1.asmx?WSDL  only. But it didnt work.

If BB does not handle the  javascript useService method what is the other way to consume web service. I am trying XMLHttpRequest method. But it is also not  working .

then is there any other alternative for consuming webservice ?

Developer
Posts: 22
Registered: ‎11-18-2009
My Device: Not Specified

Re: How to Consume Dot Net Webservice in Blackberry Widget ?

XMLHttpRequest must work.

Developer
Posts: 168
Registered: ‎11-01-2008
My Device: Galaxy S II
My Carrier: Meteor (Ireland)

Re: How to Consume Dot Net Webservice in Blackberry Widget ?

I thought all that behavio(u)r stuff was only supported on IE. W3 schools seems to agree. I'm almost certain that it's not going to work on any version of the BlackBerry browser.

 

So you should really be using XHR. What did you try with that and it what way did it not work?

New Developer
Posts: 16
Registered: ‎12-11-2009
My Device: 9500
My Carrier: IT

Re: How to Consume Dot Net Webservice in Blackberry Widget ?

hi,

now im using the following code to consume the web service. but it is not responding.

Code:


<script type="text/javascript">

 

        function openRequest()

        {

            try 

            {

                alert("Request Is Openned");

                var req = new XMLHttpRequest();

 

                req.onreadystatechange=function(){handleStateChange(req);}

                req.open("GET", "http://10.80.10.74/service.asmx",false);

               // req.open("GET", "http://10.80.10.74/service.asmx?op=HelloWorld",false);

                req.send(null);

                alert('request complete: ' + req.status);

                   if (req.status == 200) 

                    {

alert("200");

                        displayOutput(req.responseBody);

                    } 

                    else if (req.status==404)

                    {

                     alert("404");

                        displayOutput("Error: " + req.statusText);

                    }

 

                    else

                    {

                     alert("unknown");

                     displayOutput(req.responseBody);

                     }

 

 

            }

            catch(e) 

            {

alert("Catch Block Is Executed");

                alert('exception: ' + e.name + '; ' + e.message);

            }

        }

 

        function handleStateChange(xmlHttp){

          

            if(xmlHttp.readyState == 4)

{

                    alert(xmlHttp.status);

                   document.getElementById("CallDivId").innerHTML = xmlHttp.responseText;                    

            }

        }

 

        function displayOutput(val) {

            document.getElementById('CallDivId').innerHTML = val;

        }

    </script>  

</head>

<body onload="openRequest();">

<div id="CallDivId"></div>

</body>

 

 

 

Error:

 

I am getting the message that " request complete:500" (i.e. Internal Server Error) .In that service im returning Simple "Hello world". How can i solve this problem.And Here im not understanding one thing where i have to call the web service method. Please help me.


 

 

Developer
Posts: 168
Registered: ‎11-01-2008
My Device: Galaxy S II
My Carrier: Meteor (Ireland)

Re: How to Consume Dot Net Webservice in Blackberry Widget ?

OK, I think I see what's going on. The code as you've got it below is not invoking any method on the web service - it's just doing a GET on the web service itself. The default behaviour in this case is that ASP.NET will serve out the documentation page. The snag however is that for security reasons it won't do this if the requesting client is on another machine (though you can tweak web.config settings to allow this if necessary). I'm guessing that's why you're getting a 500. So you've got to at least request the actual method you want (like you are doing in the commented out line that requests the 'HelloWorld' method).

 

There is another gotcha to be aware of however. By default, ASP.NET will not allow web services to be invoked using a simple GET. You have to enable this either on a per-app basis (in the app's web.config file) or machine-wide (in the global machine.config file). It's more normal to do this in web.config. So if you want your web service to be callable using SOAP and GET you need to ensure that you've got something like the following under the <webServices> element in your web.config file:

 

 

<protocols>
    <add name="HttpSoap"/> 
    <add name="HttpGet"/> 
</protocols>

Another observation, though this shouldn't be stopping it from working, is that making synchronous requests through XHR is discouraged (you are doing this by passing false to the open() method). You are better off the rework your code to use asynchronous requests.

 

New Developer
New Developer
Posts: 17
Registered: ‎11-26-2009
My Device: Bold 9700
My Carrier: T-Mobile GER

Re: How to Consume Dot Net Webservice in Blackberry Widget ?

[ Edited ]

I've found another proberly working solution: 

 

When you are going to send the request, you have to set the request header. If the request header is not set, the server will dismiss the request. 

 

Set it something like that:

 

 

xmlHttp.open("POST", Posturl, false);

xmlHttp.setRequestHeader("Pragma", "cache"); 
xmlHttp.setRequestHeader("Cache-Control", "no-transform"); 
xmlHttp.setRequestHeader("If-Modified-Since", document.lastModified); 
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form urlencoded; charset=UTF-8"); 

xmlHttp.send("f="+x);