How to consume SOAP Web Service data with android application?

Question:

I'm developing an android application + SOAP + mysq web services, I managed to create the web services and make it run with the insertUsuario and searchTodosUsuarios methods, I consume the data and insert new ones by SoapUI, but in the android application I can only list the users, no I can make changes using the method insertUsuario of UsuarioDAO, when I try, LogCat issues the error SoapFault – FaultCode, below are the codes.

TestaWebServices is my android application, SampleWS 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 needed, I add the Web Service classes.

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

Thanks.

Answer:

Your code helped me because I wasn't getting all users.

I will pass the android app user input code 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 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;
}
Scroll to Top