View Single Post
Old 07-18-2011, 05:12 AM   #74 (permalink)
cuteguys
New Member
 
Join Date: Jul 2011
Model: 9530
PIN: N/A
Carrier: AT&T
Posts: 3
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: **** BlackBerry and Ksoap2 Tutorial *****

i am unable to see on my simulator can any one help on below code i am struggling for past two weeks...8

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) {
Ksoap2Demo theApp = new Ksoap2Demo();
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 Ksoap2DemoScreen (){

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").t oString());

}

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