View Single Post
Old 11-14-2010, 12:27 PM   #66 (permalink)
go4voip
New Member
 
Join Date: Nov 2010
Model: 7100T
PIN: N/A
Carrier: AT&T
Posts: 7
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: **** BlackBerry and Ksoap2 Tutorial *****

Hello,

I am running the below code, it runs without any error and warning but nothing display on simulator. i try to run wireshark i never found any request to my web service. Please correct me where is the wrong in below code.

Code:
package prueba;

import java.util.Vector;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.ListFieldCallback;
import net.rim.device.api.ui.container.MainScreen;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;

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

	public Ksoap2Demo() {
		pushScreen(new Ksoap2DemoScreen());		
	}

	final class Ksoap2DemoScreen extends MainScreen {
		BasicEditField symbolField;		
		ButtonField bf;
		LabelField id;
		LabelField ref;
		LabelField nombre;		
		ListField myList;
		ListCallback oList;

		public StockQuoteScreen (){
			
			symbolField = new BasicEditField ("Introduzca id: ","",5, BasicEditField.FILTER_INTEGER);
			id = new LabelField("");
			ref = new LabelField("");
			nombre = new LabelField("");
			bf = new ButtonField ("Get price", Field.FOCUSABLE);
			bf.setChangeListener(new ClickBoton());
			myList = new ListField();
			oList = new ListCallback();

			
			setTitle (symbolField);			
			add (bf);
			add(id);
			add(ref);
			add(nombre);
			
			
		}
		public class ClickBoton implements FieldChangeListener{
			public void fieldChanged(Field field, int context) {
				obtenerArticuloSOAP(symbolField.getText());
				obtenerListaArticuloSOAP("0");
			}
		}

		public boolean onClose() {
			Dialog.alert("ADIOOO!!");
			System.exit(0);
			
			return true;
			
		}
		public void obtenerArticuloSOAP(String id) {
			try {
								
				
				SoapObject request = new SoapObject("urn:DEMO", "DEMOMETHOD");			
				request.addProperty("PARAMETER_ID", id);
				
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				envelope.bodyOut = request;			
				HttpTransport ht = new HttpTransport("WEBSERVICE_URL");			
			
				//envelope.encodingStyle = SoapSerializationEnvelope.ENC;		
				
				ht.call("", envelope);	
				
				SoapObject result = (SoapObject)envelope.getResponse();
				this.id.setText(result.getProperty("id").toString());
				this.ref.setText(result.getProperty("referencia").toString());
				this.nombre.setText(result.getProperty("nombre").toString());			

			}

			catch (Exception e) {
				System.out.println("i am here in catch");
				e.printStackTrace();

			}
		}
		public void obtenerListaArticuloSOAP(String id) {
			try {
								
				
				SoapObject request = new SoapObject("urn:DEMO", "DEMOMETHOD");			
				request.addProperty("test", id);
				
				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
				envelope.bodyOut = request;			
				HttpTransport ht = new HttpTransport("localhost:1119 / Service1.asmx / MS_CurrentBalace");			
			
				//envelope.encodingStyle = SoapSerializationEnvelope.ENC;		
				
				ht.call("", envelope);	
				
				SoapObject result = (SoapObject)envelope.getResponse();				
				Vector vector = new Vector();
				for(int i=0; i < result.getPropertyCount(); i++){
					SoapObject choice = (SoapObject)result.getProperty(i);
					if(choice!=null && choice.getProperty("nombre")!=null)
						vector.addElement(new String(choice.getProperty("nombre").toString()));
								
				}
								
				oList.setVector(vector);
				for(int i=0;i<vector.size();i++){
					myList.insert(i);
				}
				myList.setCallback(oList);
				add(myList);
			}

			catch (Exception e) {
				System.out.println("i am here in catch");
				e.printStackTrace();

			}
		}
		
		
		private class ListCallback implements ListFieldCallback {
			// the listElements vector contain the entries in the list
			private Vector listElements = new Vector();

			public void drawListRow(ListField arg0, Graphics g, int index,
					int y, int w) {
				if(listElements.size()>index){
					String text = (String)listElements.elementAt(index);				
					g.drawText(text, 0, y, 0, w);					
				}
			}

			public Object get(ListField list, int index) {
				if(listElements.size()>index){
					return listElements.elementAt(index);
				}
				return null;
			}

			public int getPreferredWidth(ListField arg0) {
				// TODO Auto-generated method stub
				return Graphics.getScreenWidth();
			}

			public int indexOfList(ListField arg0, String arg1, int arg2) {
				// TODO Auto-generated method stub
				return 0;
			}
			
			public void setVector(Vector v){
				this.listElements = v;
			}
			public void insert(String toInsert, int index) {
				listElements.addElement(toInsert);
			}
			
		}
	}


}
Offline   Reply With Quote