虽然 Ksoap2 是一个很好的库,可以在 Android 应用程序开发中处理 SOAP 消息,但它没有很好的文档记录。我已经阅读了很多博客文章和问答,但信息往往不完整。是否可以提供一个完整的例子:
- 如何使用 C# 创建简单的 Web 服务?
- 如何在 Android 上使用此 Web 服务上的 Web 方法并解析返回的 SOAP 消息以生成 ListView?
虽然 Ksoap2 是一个很好的库,可以在 Android 应用程序开发中处理 SOAP 消息,但它没有很好的文档记录。我已经阅读了很多博客文章和问答,但信息往往不完整。是否可以提供一个完整的例子:
You can find how to create simple web service on google very easily.
I want to show you how to get data from .net web service:
first, you have to use ksoap2 library for getting data in android.
http://code.google.com/p/ksoap2-android/ download and copy into project>libs folder
second, you have to use asynctask for calling web service because you have to use asynctask for long time process in android.
If you download and copy ksoap2 library into project, lets get started.
//import ksoap2
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class Example extends Activity {
//define your web service namespace
private static final String NAMESPACE = "http://tempuri.org/";
//web service URL
private static final String URL = "http://www.example.com/webservice1.asmx";
//variables
private String username;
private String password;
private String userID;
public class ExampleAS extends AsyncTask<String,String,String>{
private ProgressDialog dialog = new ProgressDialog(Example.this);
String ParamUserName;
String ParamPassword;
public ExampleAS(String ParamUserName,String ParamPassword){
this.ParamUserName=ParamUserName;
this.ParamPassword=ParamPassword;
}
@Override
protected void onPreExecute() {
dialog.setMessage("Loading...");
dialog.show();
}
@Override
protected String doInBackground(String... params) {
ExampleFunc(ParamUserName,ParamPassword);
return userID;
}
protected void onPostExecute(String userID){
label.setText(userID);
dialog.dismiss();
}
}
private String ExampleFunc(String ParamUserName,String ParamPassword){
PropertyInfo uName = new PropertyInfo();
uName.name= "USERNAME"; //It must be same with your webservice's parameter name
uName.setValue(ParamUserName); //Parameter value
uName.type = PropertyInfo.STRING_CLASS; //Parameter type
PropertyInfo pass = new PropertyInfo();
pass.name= "PASSWORD"; //It must be same with your webservice's parameter name
pass.setValue(ParamPassword); //Parameter value
pass.type = PropertyInfo.STRING_CLASS; //Parameter type
SoapObject request = new SoapObject(NAMESPACE, "Login"); //Namespace,Webservice method name
request.addProperty(uName);
request.addProperty(pass);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut=request;
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call("http://tempuri.org/Login", envelope);
SoapObject response = (SoapObject) envelope.getResponse();
Object property = response.getProperty(0);
if(property instanceof SoapObject){
SoapObject users = (SoapObject) property;
userID = users.getProperty("user_id").toString();
}
}
catch (Exception e) {
e.printStackTrace();
}
return userID;
}
Don't copy and build it, It may not working because I write it on here. You should change some field for your request or getting data from web service.
You must call asynctask in onCreate
forexample;
new ExampleAS("admin","1234").execute();
finally, you should follow my another post :Get List from .net Web Service on Android