Android访问WebService-CXF步骤
来源:互联网 发布:信息技术软件应用方法 编辑:程序博客网 时间:2024/06/07 12:07
文章来源:http://blog.csdn.net/guoquanyou/article/details/9290609
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;
- }
- }
- 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();
- }
- }
- }
- <?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>
- 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);
- }
- });
- }
- }
- <!-- 设置使用web service等的权限 -->
- <uses-permission android:name="android.permission.INTERNET" />
0 0
- Android访问WebService-CXF步骤
- Android访问WebService-CXF步骤
- CXF 访问WebService 服务端方法
- QT+Gsoap访问cxf WebService
- CXF方式发布WebService全步骤
- CXF方式发布WebService全步骤
- spring和cxf整合开发Webservice步骤
- cxf发布webservice 简单使用步骤备忘
- android ksoap2调用cxf webservice
- android ksoap2调用cxf webservice
- Client端使用CXF访问WebService
- 关于cxf方式访问webservice的问题
- WebService-CXF-jQuery跨域访问
- CXF 开发webService的步骤(java First)
- CXF入门教程(3) -- webService客户端开发步骤详解
- CXF入门教程(3) -- webService客户端开发步骤详解
- CXF入门教程(3) -- webService客户端开发步骤详解
- 360doc-----CXF方式发布WebService全步骤 [未试验]
- linux 改文件文件夹名
- [迭代加深dfs] zoj 3768 Continuous Login
- hadoop操作常用命令
- java 注解
- 二维数组查找的JAVA实现
- Android访问WebService-CXF步骤
- 类目、扩展和协议
- ZOJ 3708 Density of Power Network
- HDU 1290 献给杭电五十周年校庆的礼物
- 学习总结--委托和事件
- Leetcode_max-points-on-a-line(c++ and python version)
- CodeForces 347 A. Difference Row
- 点类派生直线类
- JavaScript实现弹窗报错