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: 16,694
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Parsing xml

if you need attributes you have two options:
- patch ksoap to support attributes or find a version that does already
- don't use ksoap, use stubs.

as you don't use ksoaps serialization features anyhow i would suggest to just use stubs. or parse it all yourself.
----------------------------------------------------------
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
Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

Re: Parsing xml

Hi all,

 

   I am stuck completely...I am not very much experienced in Blackberry development....So please help me out... My code for calling web service and getting response is posted below...

 

public static Vector blnGetSeatLayout(String strCinemaCode, String strTransId1, long lngSessionId, 
											boolean blnScreenOnTop, String strMergeOption, String strAreaCats){

		final String SOAP_ACTION = "http://webser.deliver-projects.com:85/blnGetSeatLayout";
		final String METHOD_NAME = "blnGetSeatLayout";
	
		ConnectionDescriptor connDesc = getConnectionDescriptor(waveCinemaUrl);

		if (connDesc != null)
		{
			HttpConnection httpConn = null;
			try
			{
				httpConn = (HttpConnection)connDesc.getConnection();
				currentShowsFilmsByCinemaIdInfoVector = new Vector();
				HttpTransport httpt = new HttpTransport(Constants.URL + ";deviceside=true");
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				envelope.dotNet = true;
				SoapObject request = new SoapObject(Constants.NAMESPACE, METHOD_NAME);
				
				request.addProperty("strCinemaCode", strCinemaCode);
				request.addProperty("strTransId1", strTransId1);
				
				envelope.setOutputSoapObject(request); 
				httpt.call(SOAP_ACTION, envelope);
				SoapObject bodyInObj = (SoapObject)envelope.bodyIn;
								
				SoapObject blnGetSeatLayoutResult = (SoapObject) bodyInObj.getProperty("blnGetSeatLayoutResult");
				SoapObject seatLayoutNewDataSet = (SoapObject) blnGetSeatLayoutResult.getProperty("SeatLayout");
				final int totalSeatLayout = seatLayoutNewDataSet.getPropertyCount();// here it gives correct count..
				.
				.
				.
				..

 

Response form calling this web service is as follows....how can i read or parse this and use the values contained in the response..

 

<SeatLayout>
<objArea colRows="Bigtree.VistaRemote.colRows" intSeatsOrdered="0" strAreaCode="0000000003" strAreaDesc="Platinum" strAreaNum="2" blnHasCurrentOrder="False">
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="1" strRowPhyID="A" Key="R1" GetHashCode="66629781">...</colRows>
</objArea>
<objArea colRows="Bigtree.VistaRemote.colRows" intSeatsOrdered="0" strAreaCode="0000000002" strAreaDesc="Classic" strAreaNum="1" blnHasCurrentOrder="False">
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="1" strRowPhyID="P" Key="R1" GetHashCode="49382823">...</colRows>
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="2" strRowPhyID="O" Key="R2" GetHashCode="15225125">...</colRows>
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="3" strRowPhyID="N" Key="R3" GetHashCode="26458746">...</colRows>
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="4" strRowPhyID="M" Key="R4" GetHashCode="39449526">...</colRows>
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="5" strRowPhyID="L" Key="R5" GetHashCode="50346327">...</colRows>
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="6" strRowPhyID="K" Key="R6" GetHashCode="50874780">...</colRows>
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="7" strRowPhyID="J" Key="R7" GetHashCode="3318699">...</colRows>
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="8" strRowPhyID="I" Key="R8" GetHashCode="55683007">...</colRows>
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="9" strRowPhyID="H" Key="R9" GetHashCode="47005364">...</colRows>
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="10" strRowPhyID="G" Key="R10" GetHashCode="61476884">...</colRows>
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="11" strRowPhyID="F" Key="R11" GetHashCode="60052224">...</colRows>
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="12" strRowPhyID="E" Key="R12" GetHashCode="7339810">...</colRows>
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="15" strRowPhyID="D" Key="R15" GetHashCode="3137518">...</colRows>
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="16" strRowPhyID="C" Key="R16" GetHashCode="48979325">...</colRows>
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="17" strRowPhyID="B" Key="R17" GetHashCode="295723">...</colRows>
<colRows colSeats="Bigtree.VistaRemote.colSeats" intGridRowID="18" strRowPhyID="A" Key="R18" GetHashCode="10941773">...</colRows>
</objArea>
</SeatLayout>

 

Thanks in advance.

 

Warm Regards.

 

 

 

Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

Re: Parsing xml

Can anyone please tell me that Should there be any different approach  for calling the .net web service for getting the response.. As ksoap doesn't support attributes associated with a tag.. So how can i get those attributes also? Please point me somewhere i get the hint from...

 

Warm Regards

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

Re: Parsing xml

you could receive the response manually (without ksoap) and parse it with an xml parser.

----------------------------------------------------------
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
Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

Re: Parsing xml

Simon, I beg your pardon.

 

 "you could receive the response manually (without ksoap) and parse it with an xml parser."

     This is what i have been asking (because I don't know) that how could i get the response manually without using ksoap. Please...

 

Warm Regards.

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

Re: Parsing xml

if you send with ksoap you also receive with ksoap. so either remove it completely or get the response from the ServiceConnectionMidp class before ksoap can parse 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
Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

Re: Parsing xml

[ Edited ]

Hi,

 I have just been through the solution of one similar(or might be dissimilar) post http://supportforums.blackberry.com/t5/Java-Development/Http-Post-Method-is-Not-Working-in-simulator...

 

I have to make HTTP POST request and get response in the following Response format...

 

Request: 

POST /cnews.asmx/SeatLayout HTTP/1.1
Host: webservices.magno.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length

strCinews=string&strTransId1=string&lngSessionId=string&blnScreenOnTop=string&strMergeOption=string&strAreaCats=string

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

Response: 

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0"?>
xml

First of all i want to ask that caould i use this solution in my case?

 

If yes, then Here in the solution accepted by MFSI, i just want to ask that how can i pass query parameters to the web service? is this the iParams? or what? The web service requires some input parametrs mentioned above in the Request section. How should i pass these with the request?

 

Thanks and Regards

Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

Re: Parsing xml

Hi all,

 

     Thanking you. I have now been able to get the response from web service making HTTP Post request. I am posting my code here for other user's help.

 

/*
Make sure that this code should be running on separate Thread
*/

String serviceUrl = "http://webservices.magnum.com/cinws.asmx/Layout";
		URLEncodedPostData postData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
		
		postData.append("strCinewsCode", strCinewsCode);
		postData.append("strTransId", strTransId);
		postData.append("blnScreenOnTop", String.valueOf(blnScreenOnTop));
		
		
		ConnectionFactory conFactory = new ConnectionFactory();
		ConnectionDescriptor conDesc = null;
		try{
		    conDesc = conFactory.getConnection(serviceUrl);
		}catch(Exception e){
		    System.out.println(e.toString()+":"+e.getMessage());
		}
		// the below "response" variable used for the server response if we can get the connection descriptor from ConnectionFactory
		String response = ""; 
		if(null != conDesc){
		    try{
		        HttpConnection connection = (HttpConnection)conDesc.getConnection();
		        //set the header property
		        connection.setRequestMethod(HttpConnection.POST);
		        connection.setRequestProperty("Content-Length", Integer.toString(postData.size())); //body content of post data
		        connection.setRequestProperty("Connection", "close"); // close the connection after success sending request and receiving response from the server
		        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // we set the content of this request as application/x-www-form-urlencoded, because the post data is encoded as form-urlencoded(if you print the post data string, it will be like this -> q=remoQte&ie=UTF-8).
				//now it is time to write the post data into OutputStream
		        OutputStream out = connection.openOutputStream();
		        out.write(postData.getBytes());
		        out.flush();
		        out.close();
				//when this code is called, the post data request will be sent to server, and after that we can read the response from the server if the response code is 200 (HTTP OK).
		        int responseCode = connection.getResponseCode();
		        if(responseCode == HttpConnection.HTTP_OK){
		            //read the response from the server, if the response is ascii character, you can use this following code, otherwise, you must use array of byte instead of String
		            InputStream in = connection.openInputStream();
		            StringBuffer buf = new StringBuffer();
		            int read = -1;
		            while((read = in.read())!= -1){
		            	buf.append((char)read);
		            }
		            response = buf.toString();
		        }
		        System.out.println(response);
				
		        //close the connection
		    }catch(Exception e){
		        System.out.println(e.toString()+":"+e.getMessage());
		    }
		}

 

 

Warm Regards.

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

Re: Parsing xml

Thanks for this.  I would suggest two corrections:

 

1) I would use the size of your data rather than assuming that

postData.size()

is the number of bytes that you will send. The documentation says this will

"Retrieves the size of the underlying storage buffer."

which may or may not be the number of bytes you are sending. 

 

So, I would actually retrieve the bytes:

byte [] postBytes = postData.getBytes();

and then use the array to set the length:

connection.setRequestProperty("Content-Length", Integer.toString(postBytes.length)); //body content of post data

and send these bytes:

out.write(postBytes);

 

2) The following code is not particularly efficient and will also not cope with anything other than ISO-8859-1 (Latin 1) character set:


             StringBuffer buf = new StringBuffer();
            int read = -1;
            while((read = in.read())!= -1){
            buf.append((char)read);
            }
            response = buf.toString();

 

Instead I recommend you use code like the following, which is more efficient and copes with a different encoding:

 

byte [] responseBytes = IOUtilities.streamToBytes(in);

response = new String(responseBytes, "UTF-8");

 

You need to decide if UTF-8 is a correct encoding from your Server, my experience suggests that this is the most frequently used. 

 

Finally I would recommend the use of the HttpProtocolConstants Interface when setting things like

"Content-Length"

instead use:

HttpProtocolConstants.HEADER_CONTENT_LENGTH

 

This makes sure no unexpected typos cause you any issues. 

Developer
Posts: 95
Registered: ‎07-24-2013
My Device: 9380
My Carrier: Airtel

Re: Parsing xml

Thanks a lot Peter.

 

   Simply SUPERB explanation...

 

Warm Regards.