как можно потреблять веб-сервис в php с android с ksoap - PullRequest
0 голосов
/ 12 августа 2011
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="checkpoint">
   <SOAP-ENV:Body>
      <ns1:getPedidoResponse xmlns:ns1="http://schemas.xmlsoap.org/soap/envelope/">
         <tabla xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:pedidos[1]">
            <item xsi:type="tns:pedidos">
               <ID_PEDIDO xsi:type="xsd:int">1</ID_PEDIDO>
               <RUT_CLIENTE xsi:type="xsd:int">1234</RUT_CLIENTE>
               <ID_PAQUETE xsi:type="xsd:int">1</ID_PAQUETE>
               <RUT_EMPLEADO xsi:type="xsd:int">5678</RUT_EMPLEADO>
               <DIRECCION xsi:type="xsd:string">la moenda</DIRECCION>
               <LATITUD xsi:type="xsd:string">-33,44291</LATITUD>
               <lONGITUD xsi:type="xsd:string">-70,65386</lONGITUD>
               <ESTADO xsi:type="xsd:int">1</ESTADO>
            </item>
         </tabla>
      </ns1:getPedidoResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

А это мой класс:

public class Pedidos implements KvmSerializable {
    public int ID_PEDIDO;
    public    int RUT_CLIENTE;
    public    int ID_PAQUETE;
    public    int RUT_EMPLEADO;
    public    String DIRECCION;
    public    String LATITUD;
    public    String lONGITUD;
    public int ESTADO;

    public Pedidos(){}
        @Override
        public Object getProperty(int arg0) {
            switch (arg0){
                case 0:
                    return ID_PEDIDO;
                case 1:
                    return RUT_CLIENTE;
                case 2:
                    return ID_PAQUETE;
                case 3:
                    return RUT_EMPLEADO;
                case 4:
                    return DIRECCION;
                case 5:
                    return LATITUD;
                case 6:
                    return lONGITUD;
                case 7:
                    return ESTADO;
                default:
                    return null;
            }
        }

        @Override
        public int getPropertyCount() {
            return 7;
        }

        @Override
        public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
            switch (arg0){
                case 0:
                    arg2.type = PropertyInfo.INTEGER_CLASS;
                    arg2.name = "ID_PEDIDO";
                    break;
                case 1:
                    arg2.type = PropertyInfo.INTEGER_CLASS;
                    arg2.name = "RUT_CLIENTE";
                    break;
                case 2:
                    arg2.type = PropertyInfo.INTEGER_CLASS;
                    arg2.name = "ID_PAQUETE";
                    break;
                case 3:
                    arg2.type = PropertyInfo.INTEGER_CLASS;
                    arg2.name = "RUT_EMPLEADO";
                    break;
                case 4:
                    arg2.type = PropertyInfo.STRING_CLASS;
                    arg2.name = "DIRECCION";
                    break;
                case 5:
                    arg2.type = PropertyInfo.STRING_CLASS;
                    arg2.name = "LATITUD";
                    break;
                case 6:
                    arg2.type = PropertyInfo.STRING_CLASS;
                    arg2.name = "lONGITUD";
                    break;
                case 7:
                    arg2.type = PropertyInfo.INTEGER_CLASS;
                    arg2.name = "ESTADO";
                    break;
                default:
                    break;
            }
        }


        @Override
        public void setProperty(int arg0, Object arg1) {
            switch(arg0){
                case 0:
                    ID_PEDIDO = Integer.parseInt(arg1.toString());
                    break;
                case 1:
                    RUT_CLIENTE = Integer.parseInt(arg1.toString());
                case 2:
                    ID_PAQUETE = Integer.parseInt(arg1.toString());
                case 3:
                    RUT_EMPLEADO = Integer.parseInt(arg1.toString());
                case 4:
                    DIRECCION = arg1.toString();
                case 5:
                    LATITUD = arg1.toString();
                case 6:
                    lONGITUD = arg1.toString();
                case 7:
                    ESTADO = Integer.parseInt(arg1.toString());
                default:
            }
        }
}

И вот я звоню:

public class MainActivity extends Activity {
    static final String SOAP_ACTION="http://192.168.117.1/ws_checkpoint.php/getPedido";
    static final String METHOD_NAME="getPedido";
    static final String NAMESPACE="checkpoint";
    static final String URL="http://192.168.117.1/ws_checkpoint.php";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("pRutEmpleado",5678);
        SoapObject result;

        HttpTransportSE httpt = new HttpTransportSE(URL);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.addMapping(NAMESPACE, "checkpoint", Pedidos.class);
        envelope.dotNet = false;
        envelope.bodyOut = request;

        try
        {
            httpt.debug = true;
            httpt.call(SOAP_ACTION, envelope);
            result = (SoapObject)envelope.bodyIn;

            Vector<Pedidos> result_array = (Vector<Pedidos>)envelope.getResponse();

            if(result_array != null){
                for (Pedidos current_crop: result_array){
                    System.out.println(current_crop.RUT_EMPLEADO);
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }    
    }
}

Ошибка:

java.lang.ClassCastException: org.ksoap2.serialization.SoapObject 

когда я пытаюсь напечатать current_crop.RUT_EMPLEADO.

1 Ответ

1 голос
/ 12 августа 2011

Я думаю, что это связано с AndroidHttpTransport, пожалуйста, проверьте это Как вызвать .NET Webservice из Android, используя KSOAP2?

и http://seesharpgears.blogspot.com/2010/11/basic-ksoap-android-tutorial.html

Спасибовы.

...