使用kso​​p2获取数据的ANDROID

时间:2011-10-21 12:04:02

标签: android ksoap2

我在调用webservice时遇到问题。我在服务器上有一个.NET Web服务,数据是Xml格式我需要从那里检索值,我在android 2.2 galaxy附加组件中使用KSOAP2(2.5.4 jar文件)。在运行该程序时,它显示强制关闭警报消息。我的代码在下面请检查...

    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    //import org.ksoap2.serialization.SoapPrimitive;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;

    import android.app.Activity;
     import android.os.Bundle;
         import android.widget.TextView;

          public class SoapActivity extends Activity
   {
         private static String SOAP_ACTION = "http://tempuri.org/GetLabResults";

private static String NAMESPACE = "http://tempuri.org/ ";
private static String METHOD_NAME = "GetLabResults";

private static String URL = "http://192.168.1.19/MOBILETEST/service1.asmx?op=GetLabResults";


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Initialize soap request + add parameters
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);        
    request.addProperty("ReportDate","10-11-2011 10:10");


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

    // Make the soap call.
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    try {

        //this is the actual part that will call the webservice
        androidHttpTransport.call(SOAP_ACTION, envelope);        
    } catch (Exception e) {
        e.printStackTrace(); 
    }

    // Get the SoapResult from the envelope body.       
    SoapObject result = (SoapObject)envelope.bodyIn;
   // SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
   // String strRes = result.toString();


    if(result != null){
        TextView t = (TextView)this.findViewById(R.id.resultbox);
        t.setText("SOAP response:\n\n" + result.getProperty(0).toString());
    }

}

}

1 个答案:

答案 0 :(得分:2)

我有一些关于这个东西的博客文章。当您尝试使用kSOAP2和Android时,它可能对您有所帮助。

http://roderickbarnes.com/blog/category/technology/web-services-technology

我希望这会有所帮助。