1

I am trying to call java webservice form android application but unable to call it. When I generate WSDL file, SOAPAction showing blank

string(<soap:operation soapAction=""/>) in soap:operation.

My android application code:

public class MainActivity extends Activity {

private static final String SOAP_ACTION = "http://com/add";
private static final String METHOD_NAME = "add";
private static final String NAMESPACE = "http://com/";
private static final String URL = "http://localhost:8080/WebApplication1/demo";

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

        Button button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View v) {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            PropertyInfo pi1 = new PropertyInfo();
            pi1.setName("i");
            pi1.setValue(3);
            pi1.setType(int.class);
            request.addProperty(pi1);

            PropertyInfo pi2 = new PropertyInfo();
            pi2.setName("j");
            pi2.setValue(3);
            pi2.setType(int.class);
            request.addProperty(pi2);

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

            // HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);


            try {
                ((TextView) findViewById(R.id.textView1)).setText(String
                        .valueOf("The WebService is about to call"));
                androidHttpTransport.call(SOAP_ACTION, envelope);
                ((TextView) findViewById(R.id.textView1)).setText(String
                        .valueOf("The WebService call is done"));

                SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

                ((TextView) findViewById(R.id.textView1)).setText(String
                        .valueOf(response.toString()));

                ((TextView) findViewById(R.id.textView1)).setText(String
                        .valueOf("Done"));

            } catch (Exception e) {
                e.printStackTrace();
                Log.e("Err", "Error Says: " + e.toString());
            }

        }
    });
}

}

There is showing an exception network on main thread exception

Yash
  • 634
  • 1
  • 6
  • 17
  • 2
    use [Asynctask](http://developer.android.com/reference/android/os/AsyncTask.html) for making webservice call from UI thread or if you are using API LEVEl 9 or up then set [StrictMode.ThreadPolicy](http://developer.android.com/reference/android/os/StrictMode.ThreadPolicy.html) – ρяσѕρєя K Jan 28 '13 at 06:03
  • 1
    http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Anukool Jan 28 '13 at 06:15

1 Answers1

4

Because you are calling the web service in your main thread.This must be done in the background using background thread.You can use asyntask for background operations.

shubham jain
  • 156
  • 1
  • 9