Article source: blog.csdn.net/guoquanyou/…
Add the ksoap2-android-assembly-3.0.0-jar-with-dependencies. Jar package to the libs directory of the Android project. Add the WebServiceHelper class
[java] view plain copy
- 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;
- / * *
- * Access the Web Service utility class
- * @author jCuckoo
- *
- * /
- @SuppressLint(“NewApi”)
- public class WebServiceHelper {
- static {
- if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH){
- After 4.0, you need to add the following two lines of code to access the Web Service
- StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
- .detectDiskReads().detectDiskWrites().detectNetwork()
- .penaltyLog().build());
- StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
- .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
- .penaltyLog().penaltyDeath().build());
- }
- // This setting is not required for versions prior to 4.0
- }
- / * *
- * @param URL Web service path
- * @param nameSpace Specifies the Web service nameSpace
- @param methodName web service methodName
- * @param Params Web Service method parameter
- * /
- public static SoapObject getSoapObject(String serviceName,
- String methodName, String soapAction, HashMap<String, Object> params) {
- The String URL = “http://192.168.1.89:8080/MyWebService/webservice/” + + “serviceName? wsdl”;
- String NAMESPACE = “webservice.dh.com/”; // namespace, server…
- 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;
- }
- }
Create service layer LoginService\
[java] view plain copy
- 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);
- // Service name method name soapAction–null parameter data
- SoapObject soapOjbect = WebServiceHelper.getSoapObject(“UserService”, “checkUser”, null, paramsMap);
- if(soapOjbect! =null){
- Toast.makeText(context, soapOjbect.getPropertyAsString(0), Toast.LENGTH_LONG).show();
- }
- }
- }
4. Design the layout interface activity_main.xml\
[html] view plain copy
- The < LinearLayout XMLNS: android = “schemas.android.com/apk/res/and…
- xmlns:tools=”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”>
- 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”>
- 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 = “login”
- android:layout_width=”fill_parent”
- android:layout_height=”wrap_content” />
- </LinearLayout>
5. Write program main interface MainActivity\
[java] view plain copy
- 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, “username or password cannot be empty “, toast.length_long).show();
- }
- LoginService loginService=new LoginService();
- loginService.login(getApplicationContext(), userName, userPwd);
- }
- });
- }
- }
6. Set network access permissions \ in androidmanifest.xml
[html] view plain copy
- <! — Set permissions to use web services –>
- <uses-permission android:name=”android.permission.INTERNET” />
\
\