Error connecting with Webservice + Soap + Android

Asked

Viewed 164 times

0

Good afternoon, I am having a problem connecting to the webservice. I am using 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 "Sponse = (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

  • What the stack trace of Exception which is sent to catch?

  • "android.os.Networkonmainthreadexception" . NOTE: I use my mobile phone to test applications

1 answer

2


There are some errors in your code:

  1. The error is caused because you are trying to make an HTTP request on thread home. This is a serious error on Android.

  2. Your class that handles web request CallSOAP should not receive a reference to a View. In the case, the EditText edtQtde.

To solve this problem, you need to run the method Call in a background thread.

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

Using the AsyncTask, you must do the Call in the background and update your Views as a 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();
}
  • As for the view I pass as parameter, is just a gambiarra of those who have little knowledge on android, to see where the error was happening. I looked it up this morning, and I found that I needed to use threads. I solved my problem with the following lines of code: Strictmode.Threadpolicy policy = new Strictmode.ThreadPolicy.Builder(). permitAll(). build(); Strictmode.setThreadPolicy(policy);

  • However, your solution is also valid. Thank you

  • Change the StrictMode to allow requests in the main thread may allow you to execute your code, but it is definitely not the solution. This will cause you to block the main thread and can result in a bad user experience.

  • Yes Igor, from what I saw, was the worst solution to be implemented. But it will be temporary.. When it really goes to production, I will put something better

Browser other questions tagged

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