android 调用.net的web Service开发 (KSOAP2)

    网上有很多关于android 的 web service程序,对于初次在此平台上开发的人来说,仍很困难。我在网上找了个简单易懂的例子,在此和大家分享。

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

import android.app.*;
import android.os.*;
import android.widget.TextView;

public class ksoap2sample extends Activity {
    /** Called when the activity is first created. */
    private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
    private static final String METHOD_NAME = "HelloWorld";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://172.16.24.12:81/helloworld/Service1.asmx";
    TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        tv=(TextView)findViewById(R.id.textView_1);

       try {

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            //request.addProperty("prop1", "myprop");

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

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            androidHttpTransport.call(SOAP_ACTION, envelope);

            Object result = (Object)envelope.getResponse();
            System.out.println("here-->1");
            System.out.println(result.toString());
            


           //String[] results = (String[])  result.toString().split(" ");
            String[] results = result.toString().split(" ");
           System.out.println("here-->2");
           System.out.println("size = " + results.length);
           for(int i = 0; i < results.length; i++){
        	   System.out.println("" + i + " " + results[i]);
           }
            tv.setText( ""+results[0]); 
        } catch (Exception e) {
        	System.out.println("error-->2");
            tv.setText(e.getMessage());
        }
    }
}


工程还需要添加一个扩展包:ksoap2-android-assembly-2.3-jar-with-dependencies.jar

原文地址:http://stackoverflow.com/questions/1052300/how-to-call-a-net-webservice-from-android-using-ksoap2

你可能感兴趣的:(.net,android,Web,webservice,SOAP)