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

Asked

Viewed 3,146 times

3

I am developing an android application + web services SOAP + mysq, I was able to create the web services and make it run with the methods insert User and search I cannot make modifications using the insert method.

Testawebservices is my android application, Exemplows 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+"");
    }
}

That’s the way out of Logcat:

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 method to insert Your Language work? If you need to add Web Service classes.

Note: I’m sorry if I’m wrong structured codes, I tried to show the Logcat result as an error, but it got all messed up.

Thank you.

  • Where you do the http.call("urn:"+BUSCAR_TODOS, envelope); try to trade for http.call(NAMESPACE + "/" + BUSCAR_TODOS, envelope);

  • Unfortunately it did not work Murilo. Continues the same error.

1 answer

1


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

I will pass the user input code 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 user!!

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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.