http://blog.csdn.net/ice343241993/article/details/22037829
这个Demo是非常简单的一个框架。如果您需要开发的话完全可以按照这个大框架的基础上实现您的各种方法。新手可以看一下。本demo的代码书写规范完全是本人自己的习惯。服务端是:webService+cxf+Json, 客户端是android。
服务器端代码:
包结构图:
1.1javaBean的部分没有什么可说的,上代码:
CommandBean
[java] view plaincopyprint?
- public class CommandBean {
- private String status;//0 正确 -1:此用户不存在,1:用户存在, -2,数据插入失败
- private String errmsg;//错误信息
- public String getStatus() {
- return status;
- }
- public void setStatus(String status) {
- this.status = status;
- }
- public String getErrmsg() {
- return errmsg;
- }
- public void setErrmsg(String errmsg) {
- this.errmsg = errmsg;
- }
- }
UserBean
[java] view plaincopyprint?
- public class UserBean {
- private String name;
- private Integer age;
- private String account;
- private String password;
- public String getAccount() {
- return account;
- }
- public void setAccount(String account) {
- this.account = account;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public UserBean(String name, Integer age) {
- super();
- this.name = name;
- this.age = age;
- }
- public UserBean() {
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "UserBean [name=" + name + ", age=" + age + ", account="
- + account + ", password=" + password + "]";
- }
- }
1.2 cxf接口:
[java] view plaincopyprint?
- @WebService
- public interface ILoginService {
- String login(@WebParam(name = "username") String username,
- @WebParam(name = "password") String password);
- }
1.3 cxf实现类
[java] view plaincopyprint?
- @WebService(endpointInterface = "com.cxf.service.ILoginService", serviceName = "LoginServiceService")
- public class LoginService implements ILoginService {
- @Override
- public String login(String username, String password) {
- LoginHelper loginHelper = new LoginHelper();
- CommandBean commandBean = loginHelper.onLogin(username, password);
- String jsonStr = JsonOperator.toJson(commandBean);
- return jsonStr;
- }
- }
2.1.1数据库接口1
[java] view plaincopyprint?
- public interface DBService {
- void onCloseDB();//关闭数据库
- }
2.1.2数据库接口2
[java] view plaincopyprint?
- public interface LoginService {
- /**
- * 登陆
- *
- * @param account
- * 账号
- * @param password
- * 密码
- * @return
- */
- CommandBean onLogin(String account, String password);
- /**
- * 注册
- *
- * @param UserBean
- * 用户实体
- * @return
- */
- CommandBean onRegisterAccount(UserBean user);
- }
2.2.1数据库接口实现类
[java] view plaincopyprint?
- public class DBHelper implements DBService {
- private final static String URL = "jdbc:mysql://localhost:3306/workmate?user=root&password=admin";
- private Statement stmt;
- private Connection conn;
- public DBHelper() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn = DriverManager.getConnection(URL);
- stmt = conn.createStatement();
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onCloseDB() {
- if (conn != null) {
- try {
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException e) {
- // TODO: handle exception
- }
- }
- }
- public Statement getStatement() {
- return stmt;
- }
- public Connection getConnection() {
- return conn;
- }
- }
2.2.2 数据库接口实现类
[java] view plaincopyprint?
- public class LoginHelper extends DBHelper implements LoginService {
- private Connection conn;
- private Statement stmt;
- public LoginHelper() {
- conn = getConnection();
- stmt = getStatement();
- }
- @Override
- public CommandBean onLogin(String account, String password) {
- CommandBean command = new CommandBean();
- PreparedStatement ps;
- String sql = "select * from wm_t_user where account=?";
- try {
- ps = conn.prepareStatement(sql);
- ps.setString(1, account);
- ResultSet set = ps.executeQuery();
- if (set.next()) {// 用户名正确
- String pwd = set.getString("password");
- if (pwd.equals(password)) {// 密码正确
- command.setStatus("0");
- } else {// 密码不正确
- command.setStatus("-2");// 密码错误
- command.setErrmsg("密码不正确");
- }
- } else {// 不存在此用户
- command.setStatus("-1");
- command.setErrmsg("此用户不存在");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return command;
- }
- @Override
- public CommandBean onRegisterAccount(UserBean user) {
- CommandBean command = new CommandBean();
- PreparedStatement ps;
- String sql = "select * from wm_t_user where account=?";
- try {
- ps = conn.prepareStatement(sql);
- ps.setString(1, user.getAccount());
- ResultSet set = ps.executeQuery();
- if (set.next()) {// 用户存在
- command.setStatus("1");
- command.setErrmsg("此用户存在");
- } else {// 不存在此用户
- if (insertUser(user) > 0) {// 插入成功
- command.setStatus("0");
- } else {// 插入失败
- command.setStatus("-2");
- command.setErrmsg("数据库插入失败");
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return command;
- }
- private int insertUser(UserBean user) {
- String sql = "insert into wm_t_user(account,password,username,age)values(?,?,?,?)";
- PreparedStatement ps;
- int rowNo = -1;
- try {
- ps = conn.prepareStatement(sql);
- ps.setString(1, user.getAccount());
- ps.setString(2, user.getPassword());
- ps.setString(3, user.getName());
- ps.setInt(4, user.getAge());
- rowNo = ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return rowNo;
- }
- public static void main(String[] args) {
- LoginHelper db = new LoginHelper();
- UserBean user = new UserBean();
- user.setAccount("xxl6");
- user.setAge(28);
- user.setName("xxl");
- user.setPassword("0000");
- CommandBean commandBean = db.onRegisterAccount(user);
- // if(!commandBean.getStatus().equals("0")){
- // System.out.println(commandBean.getErrmsg());
- // }
- // System.out.println(commandBean.getStatus());
- }
- }
3.json解析帮助类
[java] view plaincopyprint?
- /**
- * Json与javaBean之间的转换工具类
- *
- */
- public class JsonOperator {
- /**
- * 将java对象转换成json字符串
- */
- public static <T> T json2Bean(String jsonString, Class<T> cls) {
- T t = null;
- try {
- Gson gson = new Gson();
- t = gson.fromJson(jsonString, cls);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return t;
- }
- /**
- * 将对象转换成json数据
- *
- * @param o
- * @return
- */
- public static <T> String toJson(Object o) {
- Gson gson = new Gson();
- String jsonStr = gson.toJson(o);
- return jsonStr;
- }
- /**
- * 将json数据转换成List列表返回
- *
- * @param jsonString
- * @param type
- * @return
- */
- public static <T> List<T> json2List(String jsonString, Type type) {
- List<T> list = new ArrayList<T>();
- try {
- Gson gson = new Gson();
- list = gson.fromJson(jsonString, type);
- } catch (Exception e) {
- }
- return list;
- }
- // public static void main(String[] args) {
- // String jsonStr = "[{\"name\":\"ice\",\"age\":20}]";
- // List<UserBean> list = json2List(jsonStr,
- // new TypeToken<List<UserBean>>() {
- // }.getType());
- // for (UserBean user : list)
- // System.out.println(user.getAge() + "");
- // }
- }
XML的解析本案例中没有实现。
服务端结束,在main中右键-》run as-》java application
到此服务端结束。
下面配置Android端。
下表面看一下包结构。
1javaBean是完全从服务单copy下来的。所以不列代码。
2主Activity。
[java] view plaincopyprint?
- public class MainActivity extends Activity {
- private String CLASS_NAME = getClass().getName();
- private SoapService service;
- private EditText etPwd, etAccount;
- private Handler mHandler;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initWindow();
- initHandler();
- }
- private void initHandler() {
- mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case 0:
- Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT)
- .show();
- break;
- default:
- Toast.makeText(MainActivity.this, msg.obj.toString(),
- Toast.LENGTH_SHORT).show();
- break;
- }
- }
- };
- }
- private void initWindow() {
- service = new SoapService();
- service.setNamespace("http://service.cxf.com/");
- etPwd = (EditText) findViewById(R.id.et_pwd);
- etAccount = (EditText) findViewById(R.id.et_account);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- public void login(View v) {
- service.setMethodName("login");
- HashMap<String, String> paramsMap = new HashMap<String, String>();
- paramsMap.put("username", etAccount.getText().toString().trim());
- paramsMap.put("password", etPwd.getText().toString().trim());
- service.setParams(paramsMap);
- new Thread(new Runnable() {
- @Override
- public void run() {
- Message msg = new Message();
- msg.what = 0;
- try {
- String resultJson = service.invoke();
- CommandBean command = JsonOperator.json2Bean(resultJson,
- CommandBean.class);
- msg.what = new Integer(command.getStatus());
- msg.obj = command.getErrmsg();
- mHandler.sendMessage(msg);
- } catch (XmlPullParserException e) {
- e.printStackTrace();
- msg.what = -1;
- } catch (IOException e) {
- e.printStackTrace();
- msg.what = -2;
- }
- }
- }).start();
- }
- }
3.json解析也是完全从服务端COPY过来。
4.与服务器通信类代码
[java] view plaincopyprint?
- /**
- * 与服务器通信所用
- *
- * @author Administrator
- *
- */
- public class SoapService {
- private final static String url = "http://192.168.1.40:8989/wm/login?wsdl";
- private String namespace = "http://service.cxf.com/";
- private String methodName = "";
- private Class<?> cls;
- private HashMap<String, String> paramsMap;
- public void setNamespace(String namespace) {
- this.namespace = namespace;
- }
- public void setMethodName(String methodName) {
- this.methodName = methodName;
- }
- public String invoke() throws XmlPullParserException, IOException {
- HttpTransportSE httpTransportSE = new HttpTransportSE(url);
- httpTransportSE.debug = true;
- SoapObject requestObject = new SoapObject(namespace, methodName);
- // 字段名称和get字段名称
- Iterator it = paramsMap.entrySet().iterator();
- while (it.hasNext()) {
- Map.Entry entry = (Map.Entry) it.next();
- String paramName = entry.getKey().toString();
- String value = entry.getValue().toString();
- requestObject.addProperty(paramName, value);
- }
- SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
- SoapEnvelope.VER11);
- soapEnvelope.setOutputSoapObject(requestObject);
- soapEnvelope.dotNet = false;
- String resultJsonStr = "";
- httpTransportSE.call(null, soapEnvelope);
- if (soapEnvelope.getResponse() != null) {
- SoapObject result = (SoapObject) soapEnvelope.bodyIn;
- resultJsonStr = result.getProperty("return").toString();
- }
- return resultJsonStr;
- }
- public void setParams(String className) {
- // 获取类,取得属性值
- try {
- cls = Class.forName(className);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void setParams(HashMap<String, String> paramsMap) {
- this.paramsMap = paramsMap;
- }
- }
到此客户端也添加完成。
不要忘记在AndroidManifest.xml中添加<uses-permission android:name="android.permission.INTERNET"/>权限。
在Android运行的图示:
1登陆成功。
2.用户不存在
3.密码错误。
数据库截图
源代码下载。
服务端下载
Android端下载
[java] view plaincopyprint?
- <pre code_snippet_id="255104" snippet_file_name="blog_20140325_7_5832111" name="code" class="java"><pre code_snippet_id="255104" snippet_file_name="blog_20140325_7_5832111" name="code" class="java"><pre code_snippet_id="255104" snippet_file_name="blog_20140325_7_5832111" name="code" class="java"><pre code_snippet_id="255104" snippet_file_name="blog_20140325_7_5832111" name="code" class="java"><pre code_snippet_id="255104" snippet_file_name="blog_20140325_7_5832111" name="code" class="java"><pre code_snippet_id="255104" snippet_file_name="blog_20140325_7_5832111" name="code" class="java"><pre code_snippet_id="255104" snippet_file_name="blog_20140325_7_5832111" name="code" class="java"><pre code_snippet_id="255104" snippet_file_name="blog_20140325_7_5832111" name="code" class="java"></pre><pre code_snippet_id="255104" snippet_file_name="blog_20140325_44_3151853" name="code" class="java"></pre><pre code_snippet_id="255104" snippet_file_name="blog_20140325_44_3151853" name="code" class="java"></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- </pre></pre></pre></pre></pre></pre></pre>
时间: 2024-11-10 22:19:54