Error connecting to WebService + Soap + Android

0

Good afternoon, I have a problem connecting to the webservice. I'm using the ksoap2 library.

public class CallSOAP
{
    public final String SOAP_ACTION = "http://tempuri.org/yteste";
    public  final String OPERATION_NAME = "yteste";
    public  final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
    public  final String SOAP_ADDRESS = "http://192.168.2.101/WebService.asmx";

public CallSOAP()
{
}

public String Call(EditText edtQtde)
{

    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);

    HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
    SoapPrimitive response = null;
    try
    {
        httpTransport.call(SOAP_ACTION, envelope);
        response = (SoapPrimitive)envelope.getResponse();
    }
    catch (Exception exception)
    {
        edtQtde.setText("erro do call");
        //response = exception.toString();
    }
    return response.toString();
}

The error occurs in "response = (SoapPrimitive) envelope.getResponse ();", and ends up falling in catch () where I put an error message.

Main class:

public class MainActivity extends AppCompatActivity {

Button btnAdicionar;
EditText edtQtde;

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

    btnAdicionar = (Button)findViewById(R.id.btnAdicionar);
    edtQtde = (EditText)findViewById(R.id.edtQtde);

    final AlertDialog ad=new AlertDialog.Builder(this).create();

    btnAdicionar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {

            CallSOAP cs = new CallSOAP();

            try
            {
                String resp = cs.Call(edtQtde);
                ad.setMessage(resp.toString());
            }catch(Exception ex)
            {
                ad.setMessage("erro");
            }
            ad.show();
        }
    });
}

}

Note: The yteste function only returns a string.

Thank you

    
asked by anonymous 26.01.2016 / 18:19

1 answer

2

You have some errors in your code:

  • The error is caused because you are attempting to make an HTTP request on thread principal. This is a serious error on Android.

  • Your class that takes care of the web request CallSOAP should not receive a reference to a View . In this case, EditText edtQtde.

  • To solve this problem, you need to execute the Call method on a thread background.

    I recommend you take a look at the class documentation AsyncTask or an explanation of how to run background codes .

    Using AsyncTask , you should Call in the background and update your View s in the result:

    @Override
    protected String doInBackground(Void... params) {
        CallSOAP cs = new CallSOAP();
        String resp;
        try {
            resp = cs.Call();
        } catch (Exception ex) {
            resp = "erro";
        }
        return resp;
    }
    
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        edtQtde.setText(s);
        ad.setMessage(s);
        ad.show();
    }
    
        
    27.01.2016 / 19:36