How to consume data from SOAP Web Service with android application?

3

I am developing an application android + web services SOAP + mysq, I was able to create the web services and make it run with the methods insertUsuario and searchAll users, I consume the data and insert new ones through SoapUI, but in the android application I can only list the users, I can not make modifications using the userID method, when I try, LogCat issues the SoapFault error - FaultCode, below the codes.

TestaWebServices is my android application, ExampleWS is my Web Service.

package com.example.testawebservices;
public class UsuarioDAO {
//imports emitidos.
private static final String URL = "http://192.168.0.107:8080/ExemploWS/services/UsuarioDAO?wsdl";
private static final String NAMESPACE = "http://exemploWS.videoaulazeni.com.br";

private static final String BUSCAR_TODOS ="buscarTodosUsuarios";
private static final String INSERIR = "inserirUsuario";

//Funciona 100%
public ArrayList<Usuario> buscarTodosUsuarios(){
    ArrayList<Usuario> lista = new ArrayList<Usuario>();

    SoapObject buscarUsuarios = new SoapObject(NAMESPACE, BUSCAR_TODOS);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(buscarUsuarios);

    envelope.implicitTypes = true;
    HttpTransportSE http = new HttpTransportSE(URL);

    try {
        http.call("urn:"+BUSCAR_TODOS, envelope);
        Vector<SoapObject> resposta = (Vector<SoapObject>) envelope.getResponse();

        for (SoapObject aux : resposta) {
            Usuario usr = new Usuario();
            usr.setId(Integer.parseInt(aux.getProperty("id").toString()));
            usr.setIdade(Integer.parseInt(aux.getProperty("idade").toString()));
            usr.setNome(aux.getProperty("nome").toString());

            lista.add(usr);
        }

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return lista;
}

//Não funciona
public boolean inserirUsuario(Usuario usuario){
    SoapObject inserirUsuario = new SoapObject(NAMESPACE, INSERIR);

    SoapObject usr = new SoapObject(NAMESPACE, "usuario");
    usr.addProperty("id", usuario.getId());
    usr.addProperty("nome", usuario.getNome());
    usr.addProperty("idade", usuario.getIdade());

    inserirUsuario.addSoapObject(usr);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(inserirUsuario);

    envelope.implicitTypes = true;
    HttpTransportSE http = new HttpTransportSE(URL);

    try {
        HttpTransportSE c = new HttpTransportSE(URL);
        http.call("urn:"+INSERIR, envelope);
        SoapPrimitive resposta = (SoapPrimitive) envelope.getResponse();
        return Boolean.parseBoolean(resposta.toString());

    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
 }
}

MainActivity:

package com.example.testawebservices;
//imports ocultados.
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (android.os.Build.VERSION.SDK_INT >9 ){
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }

    UsuarioDAO usuDAO = new UsuarioDAO();

    ArrayList<Usuario> lista = usuDAO.buscarTodosUsuarios();

    Log.i("ExemploWS", lista.size()+"");

    for (int i = 0; i < lista.size(); i++) {
        Log.i("ExemploWS", lista.get(i).toString());
    }

    boolean resultado = usuDAO.inserirUsuario(new Usuario(1, "Heikoz", 21));
    Log.i("ExemploWS", resultado+"");
    }
}

The output of LogCat is this:

05-02 22:21:09.700: I/ExemploWS(1113): 2
05-02 22:21:09.700: I/ExemploWS(1113): Usuario [id=10, nome=cassio, idade=0]
05-02 22:21:09.700: I/ExemploWS(1113): Usuario [id=77, nome=Fulgencio, idade=0]
05-02 22:21:09.840: W/System.err(1113): SoapFault - faultcode: 'soapenv:Server' faultstring: '1' faultactor: 'null' detail: org.kxml2.kdom.Node@41612c68
05-02 22:21:09.850: W/System.err(1113):     at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:147)
05-02 22:21:09.850: W/System.err(1113):     at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140)
05-02 22:21:09.850: W/System.err(1113):     at org.ksoap2.transport.Transport.parseResponse(Transport.java:118)
05-02 22:21:09.850: W/System.err(1113):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:275)

How do I make the insertUser method work? If I need to add the classes of the Web Service.

Note: I'm sorry if the codes were structured wrongly, I tried to show the LogCat result as an error, but it was all messed up.

Thank you.

    
asked by anonymous 03.05.2015 / 01:18

1 answer

1

Your code helped me because I was not able to find all users.

I'll pass the code to insert user of the android app that works 100%

public boolean inserirUsuario(Usuario usuario){
    SoapObject inserirUsuario = new SoapObject(NAMESPACE, INSERIR);
    SoapObject usr = new SoapObject(NAMESPACE, "usuario");
    usr.addProperty("id", usuario.getId());
    usr.addProperty("idade", usuario.getIdade());
    usr.addProperty("nome", usuario.getNome());

    inserirUsuario.addSoapObject(usr);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.setOutputSoapObject(inserirUsuario);
    envelope.implicitTypes = true;
    HttpTransportSE http = new HttpTransportSE(URL);
    try {
        http.call("urn:" + INSERIR, envelope);
        SoapPrimitive resposta = (SoapPrimitive) envelope.getResponse();
        return Boolean.parseBoolean(resposta.toString());


    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }       

}

In the web service the code in the userDAO is like this !!

public boolean inserirUsuario(Usuario usuario){
    try {
        Connection conn = ConectaMySql.obtemConexao();
        String queryInserir = "INSERT INTO 'usuario' VALUES (null, ?, ?)";
        PreparedStatement ppStm = conn.prepareStatement(queryInserir);    
        ppStm.setString(1, usuario.getNome());
        ppStm.setInt(2, usuario.getIdade());
        ppStm.executeUpdate();
        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}
    
16.06.2015 / 16:18