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.