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: 37
Registered: ‎04-27-2011
My Device: Blackberry 9800
My Carrier: AT&T
Accepted Solution

How to call Web Service through a Java Application and Parse the Response

I have currently developed a web service (http://nycews.datajump.com/ATMUtilities.asmx/GetReverseGeoCode?Lat=42.9790550&Lng=-78.7856140) that returnes an address in xml. I need to know what is the best way to call this connection and parse the xml to display the address to the user in a popupscreen.

Developer
Posts: 17,011
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to call Web Service through a Java Application and Parse the Response

you can use ksoap2 or generated stubs. you'll find plenty of posts for both search values.

the xml can be parsed using an xmlparser, again there are some sample applications and numerous posts about it.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Contributor
Posts: 37
Registered: ‎04-27-2011
My Device: Blackberry 9800
My Carrier: AT&T

Re: How to call Web Service through a Java Application and Parse the Response

Simon,

I am currently using the following in which to call the xml. I suppose this will work also but I am not sure as of yet being that I am new to blackberry development.

       String input = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
                        "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
                            "<soap:Body>"+
                                "<GetReverseGeoCode xmlns=\"http://nycews.datajump.com/\">"+
                                    "<Lat>"+LocationInfo.getLatitude().trim()+"</Lat>"+
                                    "<Lng>"+LocationInfo.getLongitude().trim()+"</Lng>"+
                                "</GetReverseGeoCode>"+
                            "</soap:Body>"+
                        "</soap:Envelope>";
        System.out.println("=============="+LocationInfo.getLatitude().trim()+"===============");
        System.out.println("=============="+LocationInfo.getLongitude().trim()+"===============");
        NetworkGetConnectionClass net = new NetworkGetConnectionClass("http://nycews.datajump.com/ATMUtilities.asmx?op=GetReverseGeoCode", input, "GetReverseGeoCode");
             
DataVector.setDataVector(null); 
DataVector.setDataVector(net.getDataVector());
        
int index=DataVector.getIndex();
        
_data=(Data)DataVector.getDataVector().elementAt(index);

 

 

Developer
Posts: 285
Registered: ‎08-06-2010
My Device: Z10

Re: How to call Web Service through a Java Application and Parse the Response

[ Edited ]

This should get you started:

 

public String getAddress() { 				
		String myURLString = "My String"
		byte[] postData = myURLString.getBytes();
		String myAddress = null; // address to return
		HttpConnection httpConnection;
		DataOutputStream os;
		Document doc;
		
		String myURLString = "http://www.google.com;interface=wifi"; // use a connection method here
		
		try {
			httpConnection = (HttpConnection) Connector.open(myURLString);
			httpConnection.setRequestMethod(HttpConnection.POST);
			httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, "application / requestJson");
			os = httpConnection.openDataOutputStream();
			os.write(postData);
			int rc = httpConnection.getResponseCode();

			if (rc != HttpConnection.HTTP_OK) {
				return;
			}
			
			httpConnection.getResponseCode();
	
	// The following code was taken from http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800599/How_To_...		
          	DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory. newInstance(); 
          	DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
          	docBuilder.isValidating();
          	doc = docBuilder.parse(httpConnection.openDataInputStream());
          	doc.getDocumentElement ().normalize ();
          	NodeList list=doc.getElementsByTagName("*");
          	_node=new String("");
          	Node tempNode = null;

	  //this "for" loop is used to parse through the
	  //XML document and extract all elements and their
	  //value, so they can be displayed on the device

          for (int i=0;i<list.getLength();i++){     	  
              Node value=list.item(i).getChildNodes().item(0);
              _node=list.item(i).getNodeName();

              if(list.item(i).hasAttributes()) {      	  
            		  tempNode = list.item(i).getAttributes().getNamedItem("address");
            		  myAddress = tempNode.getNodeValue();
              }
          }		
  
return myAddress; } catch (Exception e) { e.printStackTrace();
return ""; } }

 



Follow me on twitter @RileyGB - https://twitter.com/RileyGB
View my BB10 OpenSource projects - https://github.com/RileyGB/BlackBerry10-Samples
Contributor
Posts: 37
Registered: ‎04-27-2011
My Device: Blackberry 9800
My Carrier: AT&T

Re: How to call Web Service through a Java Application and Parse the Response

searingmedia, it is my understanding that I should be able to copy and paste the above code added and it should work with the exception of changing the URL that is being reference to. Somehow I am getting a number of errors (i.e. class or interface expected String myAddress = null; // address to return)
Highlighted
Contributor
Posts: 17
Registered: ‎06-21-2011
My Device: 9800
My Carrier: developer

Re: How to call Web Service through a Java Application and Parse the Response

SoapSerializationEnvelope envelope;
    String serviceUrl = "http://122.166.124.229/BALMWebService/Service.asmx;deviceside=true";
    String serviceNamespace = "http://122.166.124.229/BALMWebService/";
    String soapAction = "http://122.166.124.229/BALMWebService/GetTypeSummary";
    String Method_Name="GetTypeSummary";
    SoapObject rpc = new SoapObject(serviceNamespace, Method_Name);
    rpc.addProperty("flowType", FLOW_TYPE);
    rpc.addProperty("asOn", strAsOnDate);
    rpc.addProperty("currency", strCurrency);
    envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.bodyOut=rpc;
    envelope.dotNet=true;
    envelope.encodingStyle=SoapSerializationEnvelope.XSD;;
    HttpTransport ht = new HttpTransport(serviceUrl);
    ht.debug = true;
    ht.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    Object result = null;
    try
    {
    ht.call(soapAction, envelope);
  
    result=envelope.getResponse();
   
    //just_check.setText(ht.responseDump);
   
    }
    catch(Exception e)
    {
       
    }

ht.responseDump u will get the xml format of the output .that u can pass for Document 

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;

builder = factory.newDocumentBuilder();
        InputStream is = new ByteArrayInputStream(ht.responseDump
                .getBytes("UTF-8"));
        Document doc = builder.parse(is);

Contributor
Posts: 37
Registered: ‎04-27-2011
My Device: Blackberry 9800
My Carrier: AT&T

Re: How to call Web Service through a Java Application and Parse the Response

aqunkrishna, based on your code I see you are calling a soapAction. Is there a way you can explain as to exactly you are doing in the above String declaration .
Developer
Posts: 285
Registered: ‎08-06-2010
My Device: Z10

Re: How to call Web Service through a Java Application and Parse the Response

Here's a fixed version....

 

	public String getAddress() { 				
		String myString = "My String";
		byte[] postData = myString.getBytes();
		String myAddress = null; // address to return
		HttpConnection httpConnection;
		DataOutputStream os;
		Document doc;
		
		String myURLString = "http://www.google.com;interface=wifi"; // use a connection method here
		
		try {
			httpConnection = (HttpConnection) Connector.open(myURLString);
			httpConnection.setRequestMethod(HttpConnection.POST);
			httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, "application / requestJson");
			os = httpConnection.openDataOutputStream();
			os.write(postData);
			int rc = httpConnection.getResponseCode();

			if (rc != HttpConnection.HTTP_OK) {
				return "";
			}
			
			httpConnection.getResponseCode();
	
	// The following code was taken from http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800599/How_To_...		
          	DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory. newInstance(); 
          	DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
          	docBuilder.isValidating();
          	doc = docBuilder.parse(httpConnection.openDataInputStream());
          	doc.getDocumentElement ().normalize ();
          	NodeList list=doc.getElementsByTagName("*");
          	String _node = new String("");
          	Node tempNode = null;

	  //this "for" loop is used to parse through the
	  //XML document and extract all elements and their
	  //value, so they can be displayed on the device

          for (int i=0;i<list.getLength();i++){     	  
              Node value=list.item(i).getChildNodes().item(0);
              _node=list.item(i).getNodeName();

              if(list.item(i).hasAttributes()) {      	  
            		  tempNode = list.item(i).getAttributes().getNamedItem("address");
            		  myAddress = tempNode.getNodeValue();
              }
          }		
  
return myAddress;
	}
	catch (Exception e) {
		e.printStackTrace();
return "";
	}
}

 



Follow me on twitter @RileyGB - https://twitter.com/RileyGB
View my BB10 OpenSource projects - https://github.com/RileyGB/BlackBerry10-Samples
Contributor
Posts: 37
Registered: ‎04-27-2011
My Device: Blackberry 9800
My Carrier: AT&T

Re: How to call Web Service through a Java Application and Parse the Response

Searingmedia, I truly appreciate your help. I feel that I am 98% close to completing this app but can't seem to get pass this hold up of returning the reversegeocode address from the web service into a string that I can use to displayed in a popup box.

 

After copying the above code you provided. I receive the following errors:

C:\Users\ONELSON\Desktop\MobileDevl\Blackberry\SUM_ATMFinder\org\screen\DetailScreen.java:456: : expected
                 httpConnection.setRequestMethod(HttpConnection.POS?T);
                                                                     ^
C:\Users\ONELSON\Desktop\MobileDevl\Blackberry\SUM_ATMFinder\org\screen\DetailScreen.java:456: ')' expected
                 httpConnection.setRequestMethod(HttpConnection.POS?T);
                                                                      ^
C:\Users\ONELSON\Desktop\MobileDevl\Blackberry\SUM_ATMFinder\org\screen\DetailScreen.java:456: ';' expected
                 httpConnection.setRequestMethod(HttpConnection.POS?T);
                                                                       ^
C:\Users\ONELSON\Desktop\MobileDevl\Blackberry\SUM_ATMFinder\org\screen\DetailScreen.java:457: illegal start of expression
                 httpConnection.setRequestProperty(HttpProtocolCons?tants.HEADER_CONTENT_TYPE, "application / requestJson");
                               ^
C:\Users\ONELSON\Desktop\MobileDevl\Blackberry\SUM_ATMFinder\org\screen\DetailScreen.java:457: ';' expected
                 httpConnection.setRequestProperty(HttpProtocolCons?tants.HEADER_CONTENT_TYPE, "application / requestJson");
                                                  ^
C:\Users\ONELSON\Desktop\MobileDevl\Blackberry\SUM_ATMFinder\org\screen\DetailScreen.java:457: : expected
                 httpConnection.setRequestProperty(HttpProtocolCons?tants.HEADER_CONTENT_TYPE, "application / requestJson");
                                                                                             ^
C:\Users\ONELSON\Desktop\MobileDevl\Blackberry\SUM_ATMFinder\org\screen\DetailScreen.java:457: not a statement
                 httpConnection.setRequestProperty(HttpProtocolCons?tants.HEADER_CONTENT_TYPE, "application / requestJson");
                                                                   ^
C:\Users\ONELSON\Desktop\MobileDevl\Blackberry\SUM_ATMFinder\org\screen\DetailScreen.java:457: ';' expected
                 httpConnection.setRequestProperty(HttpProtocolCons?tants.HEADER_CONTENT_TYPE, "application / requestJson");
                                                                                              ^
C:\Users\ONELSON\Desktop\MobileDevl\Blackberry\SUM_ATMFinder\org\screen\DetailScreen.java:457: illegal start of expression
                 httpConnection.setRequestProperty(HttpProtocolCons?tants.HEADER_CONTENT_TYPE, "application / requestJson");
                                                                                                                          ^
C:\Users\ONELSON\Desktop\MobileDevl\Blackberry\SUM_ATMFinder\org\screen\DetailScreen.java:472: : expected
             doc = docBuilder.parse(httpConnection.openDataInputStrea?m());
                                                                         ^
C:\Users\ONELSON\Desktop\MobileDevl\Blackberry\SUM_ATMFinder\org\screen\DetailScreen.java:472: ')' expected
             doc = docBuilder.parse(httpConnection.openDataInputStrea?m());
                                                                          ^
C:\Users\ONELSON\Desktop\MobileDevl\Blackberry\SUM_ATMFinder\org\screen\DetailScreen.java:472: ';' expected
             doc = docBuilder.parse(httpConnection.openDataInputStrea?m());
                                                                           ^
C:\Users\ONELSON\Desktop\MobileDevl\Blackberry\SUM_ATMFinder\org\screen\DetailScreen.java:473: illegal start of expression
                 doc.getDocumentElement ().normalize ();
                    ^
C:\Users\ONELSON\Desktop\MobileDevl\Blackberry\SUM_ATMFinder\org\screen\DetailScreen.java:473: ';' expected
                 doc.getDocumentElement ().normalize ();
                                       ^
14 errors

 

Contributor
Posts: 37
Registered: ‎04-27-2011
My Device: Blackberry 9800
My Carrier: AT&T

Re: How to call Web Service through a Java Application and Parse the Response

searingmedia, thank you I was able to work through the compile issues which were the cause of copying and pasting the code. My issue now is that myAddress continues to return a null value. Should my process be returning a null value because it is not running in a background thread?