Android consuming web service Soap

Asked

Viewed 334 times

0

I’m developing an application to search the response server, I send the boleto number and your type. Only when I send the data he doesn’t send me a return, what I might be doing wrong?

package br.com.testes.webservicesoapxml;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;


public class MainActivity extends AppCompatActivity implements View.OnClickListener, Runnable {
private EditText edtSequencia;
private EditText edtTipo;
private Button buttonOk;
private TextView txtResultado;
private String chaveIntegracao = "sL8xlbkw2454kLx3i803981804000107Lxd5yV063sKc3gHx9344";
private Handler handler = new Handler();


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

    edtSequencia = (EditText) findViewById(R.id.edtSequencia);
    edtTipo = (EditText) findViewById(R.id.edtTipo);
    buttonOk = (Button) findViewById(R.id.buttonOk);
    txtResultado = (TextView) findViewById(R.id.txtResultado);

    buttonOk.setOnClickListener(this);


}


@Override
public void onClick(View v) {
    Thread t = new Thread();
    t.start();

}

@Override
public void run() {
    int sequencia = Integer.parseInt(edtSequencia.toString());
    String tipo = edtTipo.toString();

    WebService ws = new WebService();

    try {
      ws.enviaLinhaDigitavel(this.chaveIntegracao, sequencia, tipo);


        handler.post(new Runnable() {
            @Override
            public void run() {

            }
        });

    } catch (IOException e) {
        e.printStackTrace();
        Log.e("MainActivity", "Erro", e);
    } catch (XmlPullParserException e) {
        e.printStackTrace();
        Log.e("MainActivity", "Erro", e);
    }


}
}

package br.com.testes.webservicesoapxml;

import android.util.Log;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

/**
 * Created by rodri on 26/09/2016.
 */

public class WebService {


    public String enviaLinhaDigitavel(String chaveIntegracao, int sequencia, String tipo) throws IOException, XmlPullParserException {
        SoapObject soap = new SoapObject("urn:RouterBoxMobile", "EnviarLinhaDigitavel");
        soap.addProperty("ChaveIntegracao",chaveIntegracao);
        soap.addProperty("Sequencia", sequencia);
        soap.addProperty("tipo", tipo);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(soap);
        HttpTransportSE httpTransportSE = new HttpTransportSE("http://177.8.248.43:81/routerbox/ws_mobile/rbx_server_mobile.php?wsdl");


        Log.i("DEVMEDIA", "Chamando WebService para consulta de CEP");


        httpTransportSE.call("CadastrarAuteticacao",envelope);

        Object resultado = envelope.getResponse();


        return resultado.toString();
    }
}

  Esse é o serviço que estou trabalhando.

    <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:env="EnviaLinhaDigitavel">
   <soapenv:Header/>
   <soapenv:Body>
      <env:EnviaLinhaDigitavel soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <Autenticacao xsi:type="urn:Autenticacao" xmlns:urn="urn:RouterSistemas">
            <ChaveIntegracao xsi:type="xsd:string"></ChaveIntegracao>
         </Autenticacao>
         <DadosBoleto xsi:type="urn:DadosBoleto" xmlns:urn="urn:RouterSistemas">
            <Sequencia xsi:type="xsd:int">191</Sequencia>
            <Enviar xsi:type="xsd:string">N</Enviar>
         </DadosBoleto>
      </env:EnviaLinhaDigitavel>
   </soapenv:Body>
</soapenv:Envelope>
  • Returns an error ?

  • @Lucasqueirozribeiro Nothing has returned!

  • Strange, I tested your envelope at the address and is returning correctly I think, you came to see the envelope generated with Ksoap methods if it was the same as expected ? If yes, there may be some problem with the connection itself

  • Put some breakpoints in your code (mainly in the result.toString();) and check in the Debugger if the result object is really null, sometimes it’s something in getresponse() or toString itself()

  • @Lucasqueirozribeiro, I’m starting a few days in android development. I did a debugging but can’t see what comes in the result, how can I get the result that’s coming from the result and put in Textview? The Envelope I tested by Soapui it works. must be doing something wrong in the code.

  • @Lucasqueirozribeiro, on the Result line appears this Suspend thread.

  • 1

    By clicking next to the number of the line you add a Breakpoint, if you put a breakpoint after an action, in the debug window you can see the variable and if you click on it you see everything it has, so you can see if the answer is receiving something.

  • This Suspend Thread error can occur while trying to make an internet call on the main thread, you are doing this without any asynchronous calls ?

  • @Lucasqueirozribeiro, is without asynchronous call.

  • From API 3 you cannot make internet calls on the main thread, I will post an answer with an example and you move your call into the method.

Show 5 more comments

1 answer

1


Try moving your call to an asynchronous call:

public String enviaLinhaDigitavel(String chaveIntegracao, int sequencia, String tipo) throws IOException, XmlPullParserException {
        SoapObject soap = new SoapObject("urn:RouterBoxMobile", "EnviarLinhaDigitavel");
        soap.addProperty("ChaveIntegracao",chaveIntegracao);
        soap.addProperty("Sequencia", sequencia);
        soap.addProperty("tipo", tipo);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(soap);
        HttpTransportSE httpTransportSE = new HttpTransportSE("http://177.8.248.43:81/routerbox/ws_mobile/rbx_server_mobile.php?wsdl");






new AsyncTask<Void,String,String>(){

            @Override
            protected String doInBackground(Void... voids) {
               Log.i("DEVMEDIA", "Chamando WebService para consulta de CEP");


        httpTransportSE.call("CadastrarAuteticacao",envelope);

        Object resultado = envelope.getResponse();


        return resultado.toString();
            }

            @Override
            protected void onPostExecute(String string) {
               // faça alguma coisa com seu retorno
            }
        }.execute();

}

The only thing you need to pay attention to is that your method will no longer return the string, you need to pass the result in the method onPostExecute(), It is better to research how an Asynctask works

  • Thanks for your help, I’ll make the changes and run the tests.

Browser other questions tagged

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