View Single Post
Old 06-07-2009, 11:48 PM   #1 (permalink)
karthik25
New Member
 
Join Date: May 2009
Location: Dallas, TX
Model: 8900
PIN: N/A
Carrier: Simulator
Posts: 7
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Help needed in processing a web service response

Please Login to Remove!

Hi,

I am a beginner in BlackBerry development. I am trying to call a web service and process the response. I am successfully able to call the web service but I have trouble parsing the response. Can you please help me in deciphering what I am missing? It would do a world of good to me! Thanks in advance!!

Here is the code i am using.

Code:
import net.rim.device.api.ui.UiApplication;

public class MobileEdu extends UiApplication {
	public static void main(String[] args) {
		MobileEdu theApp = new MobileEdu();
		theApp.enterEventDispatcher();
	}
	
	public MobileEdu() {
		pushScreen(new TestScreen());
	}
}

Code:
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;
import org.kxml2.kdom.Element;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;

public class TestScreen extends MainScreen implements FieldChangeListener {
	PictureBackgroundButtonField _pButton;
	LabelField lblInfo;

	public TestScreen(){
		_pButton = new PictureBackgroundButtonField("study",Field.FOCUSABLE,"study.png");
        _pButton.setChangeListener(this);
		add(_pButton);
		
		add(new SeparatorField());
		
		lblInfo = new LabelField("");
		add(lblInfo);
	}
	
    public void fieldChanged(Field field, int context) 
    {

        String text = "";
        
        if (field instanceof PictureBackgroundButtonField) {
            text = ((PictureBackgroundButtonField)field).getText();
        }
        
        Dialog.inform(text + " was clicked.");
        
        String serviceUrl = "";
    	String serviceNamespace = "";
    	String soapAction = "";
    	String soapMethod = "GetWeatherByZipCode";

		String result = "";

		SoapObject rpc = new SoapObject(serviceNamespace, soapMethod);
		rpc.addProperty("ZipCode", "75080");

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

		envelope.bodyOut = rpc;
		envelope.dotNet = true;
		envelope.encodingStyle = SoapSerializationEnvelope.XSD;

		HttpTransport ht = new HttpTransport(serviceUrl);
		ht.debug = true;

		try
		{
			ht.call(soapAction, envelope);
			//result = (envelope.getResult()).toString();
			
			SoapObject body = (SoapObject)envelope.bodyIn;
			
			Vector wtList = (Vector)body.getProperty("Details");			
			result = Integer.toString(wtList.size());
		}
		catch(org.xmlpull.v1.XmlPullParserException ex2){
			result = "1 - " + ex2.toString();
		}
		catch(Exception ex){
			result = "2 - " + ex.toString();
		}
		
		lblInfo.setText(result);
    }
}
Web service's response:

Code:
  <?xml version="1.0" encoding="utf-8" ?> 
- <WeatherForecasts xmlns:xsi="" xmlns:xsd="" xmlns="">
  <Latitude>32.9659843</Latitude> 
  <Longitude>96.74525</Longitude> 
  <AllocationFactor>0.002192</AllocationFactor> 
  <FipsCode>48</FipsCode> 
  <PlaceName>RICHARDSON</PlaceName> 
  <StateCode>TX</StateCode> 
- <Details>
- <WeatherData>
  <Day>Sunday, June 07, 2009</Day> 
  <WeatherImage>url</WeatherImage> 
  <MaxTemperatureF>94</MaxTemperatureF> 
  <MinTemperatureF>74</MinTemperatureF> 
  <MaxTemperatureC>34</MaxTemperatureC> 
  <MinTemperatureC>23</MinTemperatureC> 
  </WeatherData>
- <WeatherData>
  <Day>Monday, June 08, 2009</Day> 
  <WeatherImage>url</WeatherImage> 
  <MaxTemperatureF>94</MaxTemperatureF> 
  <MinTemperatureF>74</MinTemperatureF> 
  <MaxTemperatureC>34</MaxTemperatureC> 
  <MinTemperatureC>23</MinTemperatureC> 
  </WeatherData>
- <WeatherData>
  <Day>Tuesday, June 09, 2009</Day> 
  <WeatherImage>url</WeatherImage> 
  <MaxTemperatureF>95</MaxTemperatureF> 
  <MinTemperatureF>76</MinTemperatureF> 
  <MaxTemperatureC>35</MaxTemperatureC> 
  <MinTemperatureC>24</MinTemperatureC> 
  </WeatherData>
- <WeatherData>
  <Day>Wednesday, June 10, 2009</Day> 
  <WeatherImage>url</WeatherImage> 
  <MaxTemperatureF>93</MaxTemperatureF> 
  <MinTemperatureF>74</MinTemperatureF> 
  <MaxTemperatureC>34</MaxTemperatureC> 
  <MinTemperatureC>23</MinTemperatureC> 
  </WeatherData>
- <WeatherData>
  <Day>Thursday, June 11, 2009</Day> 
  <WeatherImage>url</WeatherImage> 
  <MaxTemperatureF>93</MaxTemperatureF> 
  <MinTemperatureF>73</MinTemperatureF> 
  <MaxTemperatureC>34</MaxTemperatureC> 
  <MinTemperatureC>23</MinTemperatureC> 
  </WeatherData>
- <WeatherData>
  <Day>Friday, June 12, 2009</Day> 
  <WeatherImage>url</WeatherImage> 
  <MaxTemperatureF>93</MaxTemperatureF> 
  <MinTemperatureF>73</MinTemperatureF> 
  <MaxTemperatureC>34</MaxTemperatureC> 
  <MinTemperatureC>23</MinTemperatureC> 
  </WeatherData>
- <WeatherData>
  <Day>Saturday, June 13, 2009</Day> 
  <WeatherImage>url</WeatherImage> 
  <MaxTemperatureF>94</MaxTemperatureF> 
  <MinTemperatureF>73</MinTemperatureF> 
  <MaxTemperatureC>34</MaxTemperatureC> 
  <MinTemperatureC>23</MinTemperatureC> 
  </WeatherData>
  </Details>
  </WeatherForecasts>
Sorry, I had to remove the links, as I am not able to post them..

I can successfully use the getResult().toString() method to get the result and just display it. But I need to parse it as xml for processing complex return types as above. I appreciate any help as I have been breaking my head in this...

Thanks in advance

Regards,
Karthik
Offline   Reply With Quote