android 调用 webservice

最近开发一个项目需要调用webservice,找了一些资料,有几种方法,但我只这一种成功了

以.NET中的HelloWord为例,代码:

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

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

public class calltest extends Activity {
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://192.168.8.8/accept/Service1.asmx";
    TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv=(TextView)findViewById(R.id.text1);
        call();
    }

    public void call()
    {
            try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("passonString", "Rajapandian");
            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();
            tv.setText(result.toString());
            } catch (Exception e) {
            tv.setText(e.getMessage());
            }
    }
}

需要说明的是要引用一个ksoap2.jar包,我放在附件里了

当然了,AndroidManifest.xml中要加上<uses-permission android:name="android.permission.INTERNET" />


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