android客户端向服务器端验证登陆方法的实现1

遇到的问题:一个条件查询与多个条件查询,所用到的方式不一样

参考文档: http://www.oschina.net/question/1160609_133366    mybatis多条件查询的一个错误

解决方案如下:

利用序列号的方式解决多个参数的查询问题。对象与数据库关系的映射层。

层次结构如下:

android客户端向服务器端验证登陆方法的实现1

     首先定义model层里面的Userlist类,这是和我们后台mysql里面的表是一一对应的,然后定义UserlistMapper类,这个类主要是用来封装一些个方法,比如说增删改查等。其实现通过UserlistMapper.xml来实现,这个里面主要涉及一些sql语句。

      做完上面这些以后,我们就需要和客户端交互,所以定义SrvUserlist类,主要与客户端进行交互。里面有dopost与doget等方法。当然先是响应service()的方法,然后根据客户端的请求,选择不同的方法进行响应。比如说,客户端请求的是post方法,那么服务器端就需要执行dopost方法与之对应。

dao层代码:(Data access object)包含的数据的所有操作。数据访问层:

package zhuxuekui.data;
 
import java.util.List;
 
import org.apache.ibatis.annotations.Param;
 
import zhuxuekui.model.Userlist;
 
 
 
public interface UserlistMapper {
 
    Userlist selectUserlist(@Param("username")String username,@Param("passward")String passward);
}

 

参见这篇帖子:http://zhuyuehua.iteye.com/blog/1721715

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper  namespace="zhuxuekui.data.UserlistMapper">
        
        <resultMap id="userResultMap"  type="Userlist">
            <result property="username" column="username"></result>
            <result property="passward" column="passward"></result>
        </resultMap>        
        
        <select id="selectUserlist"  parameterType="Map"
            resultMap="userResultMap">
            select  * from userlist     
            where username = #{username}  and passward = #{passward}
        </select>      
    </mapper>
    

Model层:

package zhuxuekui.model;
 
public class Userlist {
 
    public String username;
    
    public String passward;
}

 

Service层:

package zhuxuekui.service;
 
import java.io.IOException;
import java.util.List;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.ibatis.builder.annotation.MapperAnnotationBuilder;
import org.apache.ibatis.session.SqlSession;
 
import zhuxuekui.data.UserlistMapper;
import zhuxuekui.model.Userlist;
 
public class SrvUserlist extends HttpServlet {
 
    private static final long serialVersionUID = 1L;
 
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        res.getWriter().println("Hello World||||!");
        super.service(req, res);
    }
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
//         String resource = "/com/arthur/data/Configuration.xml";
//         Reader reader;
//         try {
//         reader = Resources.getResourceAsReader(resource);
//         Constants.SQL_FACTORY = new SqlSessionFactoryBuilder()
//         .build(reader);
//         } catch (Exception e) {
//         e.printStackTrace();
//         e.printStackTrace(res.getWriter());
//         }
        String username = req.getParameter("username");
        String passward = req.getParameter("passward");
//        
//        System.out.println("账号: "+username);
//        System.out.println("密码:"+passward);
        
        SqlSession session = Constants.SQLFactory().openSession();
        
        //UserlistMapper  userMapper =session.getMapper(UserlistMapper.class);
      //  Userlist userlist = userMapper.selectUserlist(username);
        //加载映射 ,与 userlistMapper挂钩 ,就是一条sql语句
        UserlistMapper mapper = session.getMapper(UserlistMapper.class);
        //从数据库当中选择一条对的语句,进行比对。返回一条记录 
        Userlist userlist = mapper.selectUserlist(username,passward);
        if (userlist !=null) {
            //res.getWriter().println("there is no user with name" + username);
            String json = Constants.GSON.toJson(userlist);
            res.getWriter().println(json);
        }
        else {
            res.getWriter().println("there is new reader with name" + username);
        }
        
    }
 
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        this.doGet(req, resp);
        
        
        //String userName = req.getParameter("username");
        String username = req.getParameter("username");
        String passward = req.getParameter("passward");
        
        System.out.println("账号: "+username);
        System.out.println("密码:"+passward);
        SqlSession session = Constants.SQLFactory().openSession();
        
        UserlistMapper mapper = session.getMapper(UserlistMapper.class);
        Userlist userlist = mapper.selectUserlist(username,passward);
        if (userlist != null) {
            resp.setStatus(200);
        } else {
            resp.sendError(345, "用户名密码不正确!");
        }
    }
}
 

服务器端调试界面:

android客户端向服务器端验证登陆方法的实现1

客户端代码:

package com.buu.play.aa;
 
import org.apache.http.Header;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;
 
import com.buu.play.BMapApiDemoMain;
import com.buu.play.DemoApplication;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.BaseJsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
 
public class LoginActivity extends Activity implements OnClickListener {
 
    static final String TAG = "ahttp";
    private EditText etName, etPsw;
    private Button btnLogin;
    AsyncHttpClient httpClient;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        httpClient = DemoApplication.instance().asyncHttpClient;
 
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        ll.setPadding(25, 25, 25, 25);
        setContentView(ll);
 
        etName = new EditText(this);
        etName.setHint("用户名");
        ll.addView(etName, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
 
        etPsw = new EditText(this);
        etPsw.setHint("密码");
        ll.addView(etPsw, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
 
        btnLogin = new Button(this);
        btnLogin.setText("登 录");
        btnLogin.setOnClickListener(this);
        ll.addView(btnLogin, LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
 
    }
 
    @Override
    public void onClick(View v) {
 
        // Intent intent = new Intent(LoginActivity.this,
        // BMapApiDemoMain.class);
        // LoginActivity.this.startActivity(intent);
 
        if (!isValid()) {
            Toast.makeText(LoginActivity.this, "用户名密码没有填写", Toast.LENGTH_LONG)
                    .show();
            // 用户名密码没有填写
            return;
        }
        //
        // TODO do request
        RequestParams param = new RequestParams();
        param.put("username", etName.getText().toString());
        param.put("passward", etPsw.getText().toString());
        httpClient.post("http://192.168.1.106:8080/login", param,
                new BaseJsonHttpResponseHandler<Object>() {
 
                    @Override
                    public void onSuccess(int statusCode, Header[] headers,
                            String rawJsonResponse, Object response) {
                        Log.d(TAG, rawJsonResponse);
                        Intent intent = new Intent(LoginActivity.this,
                                BMapApiDemoMain.class);
                        LoginActivity.this.startActivity(intent);
                        finish();
                    }
 
                    @Override
                    public void onFailure(int statusCode, Header[] headers,
                            Throwable throwable, String rawJsonData,
                            Object errorResponse) {
                        // Intent intent = new Intent(LoginActivity.this,
                        // BMapApiDemoMain.class);
                        // LoginActivity.this.startActivity(intent);
                        // finish();
                        Toast.makeText(LoginActivity.this, "用户名密码错误",
                                Toast.LENGTH_LONG).show();
                    }
 
                    @Override
                    protected Object parseResponse(String rawJsonData,
                            boolean isFailure) throws Throwable {
                        Log.d(TAG, rawJsonData);
                        return rawJsonData;
                    }
                });
    }
 
    private boolean isValid() {
        return etName.getText().length() > 0 && etPsw.getText().length() > 0;
    }
}

改进:1)客户端:没有做注册,只做了一个登陆。后期可能需要优化。

         2)服务器端:只尝试了序列号的方法,后期尝试hashmap方法。

android客户端向服务器端验证登陆方法的实现1,布布扣,bubuko.com

android客户端向服务器端验证登陆方法的实现1

上一篇:iOS开发中一些有用的小代码


下一篇:day5_python学习笔记_chapter6_字符串列表元组