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
Developer
Posts: 51
Registered: ‎10-21-2008
My Device: Bold 9000
My Carrier: Dialog

Web Service Access

Hi,

 

I have to develop a blackberry java application which access PHP web service. Anybody please tell me what are the available ways to do that. And what is the most recommended method.

 

Thank you,

 

Prasad.

Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: Web Service Access

I'm not sure how to access a PHP Web Service. I can help you with Java or .NET web services... In any case, I'd suggest doing some research on JSR 172 and kSOAP2 ( API ).

 

Hope that helps,

~Dom

 

P.S. - I've got plenty of code using kSOAP2, if you can use it for your purposes. Let me know and I'll post an example.

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
New Developer
Posts: 23
Registered: ‎10-30-2008
My Device: Not Specified

Re: Web Service Access

if you use blackberry os 4.5, you can use sun wireless toolkits to generate the stub, it is fair easy to use and compatible with w3 standard web servicve.

Developer
Posts: 51
Registered: ‎10-21-2008
My Device: Bold 9000
My Carrier: Dialog

Re: Web Service Access

Hi,

 

Such a great help.

 

Appriciate if you can send me some examples. Better if it is simple and object oriented.

 

Thank you so much.

 

Prasad.

Developer
Posts: 51
Registered: ‎10-21-2008
My Device: Bold 9000
My Carrier: Dialog

Re: Web Service Access

Hi,

 

Thanks for your reply.

 

I am using JDE 5.0. I  thought it should have some easy way to call web services. But I couldn't find.

 

Again thank you very much for sharing your knowledge.

 

Prasad.

Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: Web Service Access

The following works for a BlackBerry Application calling a .NET web service:

 

 

    String methodName = "Adder";
    String IP = "http://68.179.26.101/";
    String namespace = "http://dominic_aquilina.org/";
    String qualifiedPath = "daservice/xtest.asmx";

    try
    {
      SoapObject s = new SoapObject (namespace, methodName);
      s.newInstance ();
      s.addProperty ("a", "5");
      s.addProperty ("b", "6");
      
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope 
                                           (SoapEnvelope.VER11);
      envelope.bodyOut = s;
      envelope.dotNet = true;
      envelope.encodingStyle = "utf-8";
      envelope.enc = SoapSerializationEnvelope.ENC2001;
      envelope.xsd = SoapEnvelope.XSD;
      envelope.xsi = SoapEnvelope.XSI;
      
      //Build connection
      HttpTransport transport = new HttpTransport
                                (IP + qualifiedPath);
      
      //Initiate Connection
      String response;
      
      try 
      {
        transport.debug = true;
        
        transport.call (namespace + methodName, envelope);
        
        response = envelope.getResponse ().toString ();
        
        //Debug
        System.out.println (transport.responseDump);
        System.out.println (transport.requestDump);
        System.out.println (envelope.bodyIn);
      }
      catch (Exception e)
      {
        System.out.println (e);
      }
    }
    catch (Exception e)
    {
      System.out.println (e);
    }
    

 

 

This is probably the simplest example I have. There's a bunch more stuff with building request properties manually, and SoapPrimitive objects for simple types, but this should serve you well. That said, 2 points:

 

a) I'm not sure if this web service is still up. You could probably run this and get results if you want to test, but if you can't, then it's been removed from the server.

b) I highly doubt the same code will work for a PHP request.

 

As for direct calling...

 

There's a method of calling a web service by querying a page directly and getting a return value, as shown below. Note: This code will NOT function. I'm removing important details for security reasons. The logic is all there, but the path is wrong.

 

 

    String IP = "http://##.##.##.##/";
    String path = "folder/page.aspx";
    String query = "?num1=10&num2=20";
    
    HttpConnection getID;
    
    try
    {
      //Open a connection to the website
      getSum = (HttpConnection) Connector.open (IP + path + query);
      
      //Open a stream for receiving the box ID
      DataInputStream Num_in = getSum.openDataInputStream ();
      
      //Get the Sum
      byte [] buffer = new byte [Num_in.available ()];
      int length = Num_in.read (buffer);
      
      //Convert the received digits (char) to a String for parsing
      String str = "";
      
      for (int count = 0; count < length; count++)
      {
        str += (char) buffer [count];
      }
      
      num3 = Integer.parseInt (str);
      
      //Debug
      Assert (buffer.toString ());
      Assert (Integer.toString (ID));
      
      //Complete operation; Close website connection
      getSum.close ();
    }
    catch (Exception e)
    {
      System.out.println (e);
      log (e.toString ());
    }

 

 

Hope that helps,

~Dom

 

P.S. - I'm still very inexperienced with web services. For this reason, my knowledge of the deeper functioning of the calls above is severely limited. I can't tell you much more than syntax. Sorry.

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
Developer
Posts: 51
Registered: ‎10-21-2008
My Device: Bold 9000
My Carrier: Dialog

Re: Web Service Access

Hi Dom,

 

Thank you so much for your great help.

 

I tried with your given code by changing web service, method and paramete settings. It was compiled without any error. But when I try that with the emulator I got following error.

 

My program name is "webservice"

 

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

 

Starting webservice
Module 'webservice' has verification errors.
Error starting webservice: Module 'webservice' has verification errors.

 

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

 

Then I searched the internet and found that happened because of preveryfication problem. So I tried to execute preverification manually. But in that case I am getting following error.

 

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

 

C:\>"Program Files\Research In Motion\BlackBerry JDE 5.0.0\bin\preverify" "D:\Office\Blackberry Development\Web Service\ksoap2-j2me-core-2.1.2.jar"
Error preverifying class org.kobjects.base64.Base64
    java/lang/NoClassDefFoundError: java/lang/Object

 

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

 

Do you have any idea about this problem. I am using ksoap2.

 

Again thank you very much.

 

Prasad.

Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: Web Service Access

Right, I should have sent this link first...

 

Craig A. Green's Blog - BlackBerry and .NET Web Services

 

You can't preverify the jar yourself, but they have a version which has already been preverified. It can be found in the above link, along with a very helpful tutorial on setting up your JDE to work with kSOAP2.

 

Hope that helps,

~Dom

----------------------------------------------------------------------------
chown -R us ./base
~J!NX