无法从Android应用程序调用Java Webservice

时间:2013-01-28 06:00:02

标签: android web-services

我试图调用java webservice表单的android应用程序但无法调用它。当我生成WSDL个文件时,SOAPAction显示空白

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

我的Android应用程序代码:

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());
            }

        }
    });
}

}

显示异常network on main thread exception

1 个答案:

答案 0 :(得分:4)

因为您在主线程中调用Web服务。这必须在后台使用后台线程完成。您可以使用asyntask进行后台操作。

相关问题