1

我正在尝试从 android 应用程序调用 java webservice 但无法调用它。当我生成WSDL文件时,SOAPAction显示空白

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

我的安卓应用程序代码:

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

4

1 回答 1

4

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

于 2013-07-10T06:28:20.120 回答