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

  1. import java.util.HashMap;  
  2. import java.util.Map.Entry;  
  3. import org.ksoap2.SoapEnvelope;  
  4. import org.ksoap2.serialization.SoapObject;  
  5. import org.ksoap2.serialization.SoapSerializationEnvelope;  
  6. import org.ksoap2.transport.HttpTransportSE;  
  7. import android.annotation.SuppressLint;  
  8. import android.os.Build;  
  9. import android.os.StrictMode;  
  10. / * *
  11. * Access the Web Service utility class
  12.  * @author jCuckoo 
  13.  *  
  14. * /
  15. @SuppressLint(“NewApi”)  
  16. public class WebServiceHelper {  
  17.     static {  
  18.         if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH){  
  19. After 4.0, you need to add the following two lines of code to access the Web Service
  20.             StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()  
  21.                     .detectDiskReads().detectDiskWrites().detectNetwork()  
  22.                     .penaltyLog().build());  
  23.   
  24.             StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()  
  25.                     .detectLeakedSqlLiteObjects().detectLeakedClosableObjects()  
  26.                     .penaltyLog().penaltyDeath().build());  
  27.         }  
  28. // This setting is not required for versions prior to 4.0
  29.     }  
  30. / * *
  31. * @param URL Web service path
  32. * @param nameSpace Specifies the Web service nameSpace
  33. @param methodName web service methodName
  34. * @param Params Web Service method parameter
  35. * /
  36.     public static SoapObject getSoapObject(String serviceName,  
  37.             String methodName, String soapAction, HashMap<String, Object> params) {  
  38. The String URL = “http://192.168.1.89:8080/MyWebService/webservice/” + + “serviceName? wsdl”;
  39. String NAMESPACE = “webservice.dh.com/”; // namespace, server…
  40.         String METHOD_NAME = methodName;  
  41.         String SOAP_ACTION = soapAction;  
  42.   
  43.         SoapObject soap = null;  
  44.         try {  
  45.             SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);  
  46. if (params ! = null && params.size() > 0) {
  47.                 for (Entry<String, Object> item : params.entrySet()) {  
  48.                     rpc.addProperty(item.getKey(), item.getValue().toString());  
  49.                 }  
  50.             }  
  51.   
  52.             SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);  
  53.             envelope.bodyOut = rpc;  
  54. envelope.dotNet = false; // true–net; false–java;
  55.             envelope.setOutputSoapObject(rpc);  
  56.   
  57.             HttpTransportSE ht = new HttpTransportSE(URL);  
  58.             ht.debug = true;  
  59.             ht.call(SOAP_ACTION, envelope);  
  60.             try {  
  61.                 soap = (SoapObject) envelope.getResponse();  
  62.             } catch (Exception e) {  
  63.                 soap = (SoapObject) envelope.bodyIn;  
  64.             }  
  65.         } catch (Exception ex) {  
  66.             ex.printStackTrace();  
  67.         }  
  68.         return soap;  
  69.     }  
  70. }  

Create service layer LoginService\

[java]  view plain copy

  1. import java.util.HashMap;  
  2. import org.ksoap2.serialization.SoapObject;  
  3. import android.content.Context;  
  4. import android.widget.Toast;  
  5.   
  6. import com.dh.util.WebServiceHelper;  
  7.   
  8. public class LoginService {  
  9.     public void login(Context context,String userName,String userPwd){  
  10.         HashMap<String, Object> paramsMap = new HashMap<String, Object>();  
  11.         paramsMap.put(“userName”, userName);  
  12.         paramsMap.put(“userPwd”, userPwd);  
  13. // Service name method name soapAction–null parameter data
  14.         SoapObject soapOjbect = WebServiceHelper.getSoapObject(“UserService”, “checkUser”, null, paramsMap);  
  15. if(soapOjbect! =null){
  16.             Toast.makeText(context, soapOjbect.getPropertyAsString(0), Toast.LENGTH_LONG).show();  
  17.         }  
  18.     }  
  19. }  

4. Design the layout interface activity_main.xml\

[html]  view plain copy

  1. The < LinearLayout XMLNS: android = “schemas.android.com/apk/res/and…
  2.     xmlns:tools=”schemas.android.com/tools”  
  3.     android:layout_width=”match_parent”  
  4.     android:layout_height=”match_parent”  
  5.     android:paddingBottom=”@dimen/activity_vertical_margin”  
  6.     android:paddingLeft=”@dimen/activity_horizontal_margin”  
  7.     android:paddingRight=”@dimen/activity_horizontal_margin”  
  8.     android:paddingTop=”@dimen/activity_vertical_margin”  
  9.     tools:context=”.MainActivity”   
  10.     android:orientation=”vertical”>  
  11.     <LinearLayout android:id=”@+id/Layout_top”  
  12.         android:layout_width=”wrap_content”  
  13.         android:layout_height=”wrap_content”  
  14.         android:orientation=”horizontal”>  
  15.             android:layout_width=”fill_parent”  
  16.             android:layout_height=”wrap_content”/>  
  17.         <EditText android:id=”@+id/userName”  
  18.             android:layout_width=”fill_parent”  
  19.             android:layout_height=”wrap_content”/>  
  20.     </LinearLayout>  
  21.     <LinearLayout android:id=”@+id/Layout_middle”  
  22.         android:layout_width=”wrap_content”  
  23.         android:layout_height=”wrap_content”  
  24.         android:orientation=”horizontal”>  
  25.             android:layout_width=”fill_parent”  
  26.             android:layout_height=”wrap_content”/>  
  27.         <EditText android:id=”@+id/userPwd”  
  28.             android:layout_width=”fill_parent”  
  29.             android:layout_height=”wrap_content”/>  
  30.     </LinearLayout>  
  31.     <Button android:id=”@+id/login_Button”  
  32. Android: text = “login”
  33.         android:layout_width=”fill_parent”  
  34.         android:layout_height=”wrap_content” />  
  35. </LinearLayout>  

5. Write program main interface MainActivity\

[java]  view plain copy

  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.Menu;  
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8. import android.widget.Toast;  
  9.   
  10. import com.dh.service.LoginService;  
  11.   
  12. public class MainActivity extends Activity {  
  13.     private EditText userNameEditText;  
  14.     private EditText userPwdEditText;  
  15.     private Button loginButton;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         userNameEditText=(EditText) findViewById(R.id.userName);  
  21.         userPwdEditText=(EditText)findViewById(R.id.userPwd);  
  22.         loginButton=(Button) findViewById(R.id.login_Button);  
  23.         loginButton.setOnClickListener(new OnClickListener() {  
  24.               
  25.             @Override  
  26.             public void onClick(View arg0) {  
  27.                 String userName=userNameEditText.getText().toString();  
  28.                 String userPwd=userPwdEditText.getText().toString();  
  29.                 if(“”.equals(userName)&&””.equals(userPwd)){  
  30. Toast.maketext (mainactivity.this, “username or password cannot be empty “, toast.length_long).show();
  31.                 }  
  32.                 LoginService loginService=new LoginService();  
  33.                 loginService.login(getApplicationContext(), userName, userPwd);  
  34.                   
  35.             }  
  36.         });  
  37.     }  
  38. }  

6. Set network access permissions \ in androidmanifest.xml

[html]  view plain copy

  1. <! — Set permissions to use web services –>
  2.     <uses-permission android:name=”android.permission.INTERNET” />  

\

\