1

我想调用 Web 服务来获取一些必须显示在我的第一个屏幕中的数据。我用过OnCreate(),它抛出了一些异常。如何在OnCreate()方法中调用Web Service?

4

2 回答 2

1

因此,您想从 OnCreate() 调用 Web 服务,而不是像这样使用:

String NAMESPACE = "Target Name Sapce/";
String URL = "URL generated in WSDL";
String SOAP_ACTION = "Name Sapce/Method name";
String METHOD_NAME = "Method Name";

//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);       

//Use this to add parameters
request.addProperty("Parameter1",Parameter1);
request.addProperty("Parameter2",Parameter2);

//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {              
    androidHttpTransport.debug = true;
    //this is the actual part that will call the  
    androidHttpTransport.call(SOAP_ACTION, envelope);
    // Get the SoapResult from the envelope body.
    SoapObject result = (SoapObject)envelope.bodyIn;
    String validate = result.getProperty(0).toString();

    //Get the first property and change the label text
    Toast.makeText(getApplicationContext(), "Connected "+result.getProperty(0).toString(),Toast.LENGTH_LONG).show();

} catch (Exception e) {
      e.printStackTrace();
}

这将帮助您从 OnCreate() 调用 Web 服务而不会出错。

于 2013-01-03T09:12:42.023 回答
0

无论是否在onCreate(),您都不应该在ui 线程内建立网络。如果你想从网络上获取数据,你可以使用这个基于回调的库。覆盖它onStart()以显示一些“请稍候...”的东西,并覆盖它的onSuccess(),onFailure()onFinish()(如果需要)以继续执行。

于 2013-01-03T08:50:32.630 回答