|
代码示例: package com.study.ws;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class WSClient extends Activity {
private Button search;
private EditText phone;
private TextView result;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
search=(Button)findViewById(R.id.search);
phone=(EditText)findViewById(R.id.phone);
result=(TextView)findViewById(R.id.result);
search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String strPhone=phone.getText().toString().trim();
if(!(strPhone.length()==0||strPhone.equals("")))
{
String str = "";
try {
str = getPhoneInfo(strPhone);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
result.setText(str);
}
}
});
}
public String getPhoneInfo(String phoneName) throws IOException, XmlPullParserException
{
//返回的查询结果
String result = null;
//调用webservice接口的名称空间
String nameSpace="http://WebXml.com.cn/";
//调用的方法名
String methodName="getMobileCodeInfo";
//将方法名和名称空间绑定在一起
String SOAP_ACTION = nameSpace + methodName;
//获得返回请求对象
SoapObject request = new SoapObject(nameSpace, methodName);
//设置需要返回请求对象的参数
request.addProperty("mobileCode", phoneName);
request.addProperty("userId", "");
//设置soap的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
//设置是否调用的是dotNet开发的
envelope.dotNet = true;
//
envelope.bodyOut = request;
AndroidHttpTransport hts = new AndroidHttpTransport
("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
// web service请求
hts.call(SOAP_ACTION, envelope);
// 得到返回结果
Object o = envelope.getResponse();
result = o.toString();
return result;
}
}
复制代码 |
|