1.将ksoap2-android-assembly-3.0.0-jar-with-dependencies.jar包添加到Android项目的libs目录下
2.Web Service的工具类WebServiceHelper
import java.util.HashMap;
import java.util.Map.Entry;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.StrictMode;
/**
* 访问Web Service的工具类
* @author jCuckoo
*
*/
@SuppressLint("NewApi")
public class WebServiceHelper {
static {
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH){
// 4.0以后需要加入下列两行代码,才可以访问Web Service
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
.penaltyLog().penaltyDeath().build());
}
//4.0以前版本不需要以上设置
}
/**
* @param url web service路径
* @param nameSpace web service名称空间
* @param methodName web service方法名称
* @param params web service方法参数
*/
public static SoapObject getSoapObject(String serviceName,
String methodName, String soapAction, HashMap<String, Object> params) {
String URL = "http://192.168.1.89:8080/MyWebService/webservice/"+ serviceName + "?wsdl";
String NAMESPACE = "http://webservice.dh.com/";// 名称空间,服务器端生成的namespace属性值
String METHOD_NAME = methodName;
String SOAP_ACTION = soapAction;
SoapObject soap = null;
try {
SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
if (params != null && params.size() > 0) {
for (Entry<String, Object> item : params.entrySet()) {
rpc.addProperty(item.getKey(), item.getValue().toString());
}
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = false;// true--net; false--java;
envelope.setOutputSoapObject(rpc);
HttpTransportSE ht = new HttpTransportSE(URL);
ht.debug = true;
ht.call(SOAP_ACTION, envelope);
try {
soap = (SoapObject) envelope.getResponse();
} catch (Exception e) {
soap = (SoapObject) envelope.bodyIn;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return soap;
}
}
3.创建service层LoginService
import java.util.HashMap;
import org.ksoap2.serialization.SoapObject;
import android.content.Context;
import android.widget.Toast;
import com.dh.util.WebServiceHelper;
public class LoginService {
public void login(Context context,String userName,String userPwd){
HashMap<String, Object> paramsMap = new HashMap<String, Object>();
paramsMap.put("userName", userName);
paramsMap.put("userPwd", userPwd);
// 服务的名称 方法名 soapAction--null 参数数据
SoapObject soapOjbect = WebServiceHelper.getSoapObject("UserService", "checkUser", null, paramsMap);
if(soapOjbect!=null){
Toast.makeText(context, soapOjbect.getPropertyAsString(0), Toast.LENGTH_LONG).show();
}
}
}
4.设计布局界面activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout android:id="@+id/Layout_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:text="用户名:"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/userName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:id="@+id/Layout_middle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:text="密码:"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText android:id="@+id/userPwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<Button android:id="@+id/login_Button"
android:text="登陆"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
5.编写程序主界面MainActivity
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.dh.service.LoginService;
public class MainActivity extends Activity {
private EditText userNameEditText;
private EditText userPwdEditText;
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userNameEditText=(EditText) findViewById(R.id.userName);
userPwdEditText=(EditText)findViewById(R.id.userPwd);
loginButton=(Button) findViewById(R.id.login_Button);
loginButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String userName=userNameEditText.getText().toString();
String userPwd=userPwdEditText.getText().toString();
if("".equals(userName)&&"".equals(userPwd)){
Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_LONG).show();
}
LoginService loginService=new LoginService();
loginService.login(getApplicationContext(), userName, userPwd);
}
});
}
}
6.在AndroidManifest.xml中设定网络访问权限
<!-- 设置使用web service等的权限 -->
<uses-permission android:name="android.permission.INTERNET" />