根据实习科目需求,先完成Android客户端的登录功能。
要实现登录必然涉及到客户端与服务器端的交互,即客户端需要将用户输入的账号和密码传给服务器端,服务器端连接数据库对其进行验证。
细化问题,先研究一下客户端向服务器端传值的方法。。
新建一个web工程,建一个Servlet如下:
- public class UserServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public UserServlet() {
- super();
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String userName=request.getParameter("userName");
- String passWord=request.getParameter("passWord");
- //在服务器端解决中文乱码问题
- userName=NewString.getNewString(userName);
- passWord=NewString.getNewString(passWord);
- System.out.println("账号:"+userName);
- System.out.println("密码:"+passWord);
- }
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- this.doGet(request, response);
- }
- }
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public UserServlet() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName=request.getParameter("userName");
String passWord=request.getParameter("passWord");
//在服务器端解决中文乱码问题
userName=NewString.getNewString(userName);
passWord=NewString.getNewString(passWord);
System.out.println("账号:"+userName);
System.out.println("密码:"+passWord);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
客户端登录界面如下,布局比较简单。
Activity代码:
- public class LoginActivity extends Activity {
- private EditText userName;
- private EditText passWord;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- userName=(EditText)this.findViewById(R.id.userName);
- passWord=(EditText)this.findViewById(R.id.passWord);
- }
- /**
- * 用户登录的方法
- */
- public void login(View v)
- {
- //取得用户输入的账号和密码
- String name=userName.getText().toString();
- String pass=passWord.getText().toString();
- boolean result=UserService.check(name,pass);
- if(result)
- {
- Toast.makeText(getApplicationContext(),R.string.success,1).show();
- }else
- {
- Toast.makeText(getApplicationContext(),R.string.fail,1).show();
- }
- }
- }
public class LoginActivity extends Activity {
private EditText userName;
private EditText passWord;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName=(EditText)this.findViewById(R.id.userName);
passWord=(EditText)this.findViewById(R.id.passWord);
}
/**
* 用户登录的方法
*/
public void login(View v)
{
//取得用户输入的账号和密码
String name=userName.getText().toString();
String pass=passWord.getText().toString();
boolean result=UserService.check(name,pass);
if(result)
{
Toast.makeText(getApplicationContext(),R.string.success,1).show();
}else
{
Toast.makeText(getApplicationContext(),R.string.fail,1).show();
}
}
}
具体实现的工具类:
- public class UserService {
- /**
- * 验证用户登录是否合法
- * 返回值:请求是否成功
- */
- public static boolean check(String name, String pass) {
- String path="http://135.32.89.17:8080/lss/UserServlet";
- //将用户名和密码放入HashMap中
- Map<String,String> params=new HashMap<String,String>();
- params.put("userName", name);
- params.put("passWord", pass);
- try {
- return sendGETRequest(path,params,"UTF-8");
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
- private static boolean sendGETRequest(String path,
- Map<String, String> params,String encode) throws MalformedURLException, IOException {
- StringBuilder url=new StringBuilder(path);
- url.append("?");
- for(Map.Entry<String, String> entry:params.entrySet())
- {
- url.append(entry.getKey()).append("=");
- url.append(URLEncoder.encode(entry.getValue(),encode));
- url.append("&");
- }
- //删掉最后一个&
- url.deleteCharAt(url.length()-1);
- HttpURLConnection conn=(HttpURLConnection)new URL(url.toString()).openConnection();
- conn.setConnectTimeout(5000);
- conn.setRequestMethod("GET");
- if(conn.getResponseCode()==200)
- {
- return true;
- }
- return false;
- }
- }
public class UserService {
/**
* 验证用户登录是否合法
* 返回值:请求是否成功
*/
public static boolean check(String name, String pass) {
String path="http://135.32.89.17:8080/lss/UserServlet";
//将用户名和密码放入HashMap中
Map<String,String> params=new HashMap<String,String>();
params.put("userName", name);
params.put("passWord", pass);
try {
return sendGETRequest(path,params,"UTF-8");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
private static boolean sendGETRequest(String path,
Map<String, String> params,String encode) throws MalformedURLException, IOException {
StringBuilder url=new StringBuilder(path);
url.append("?");
for(Map.Entry<String, String> entry:params.entrySet())
{
url.append(entry.getKey()).append("=");
url.append(URLEncoder.encode(entry.getValue(),encode));
url.append("&");
}
//删掉最后一个&
url.deleteCharAt(url.length()-1);
HttpURLConnection conn=(HttpURLConnection)new URL(url.toString()).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode()==200)
{
return true;
}
return false;
}
}
运行结果:
服务器端接收内容:
总结:
客户端的工具类将用户输入的账号和密码放入HashMap当中,循环遍历取出HashMap中的键和值,构造出形如
http://www.xxx.xxx?key1=value1&key2=value2的字符串。利用该字符串连接服务器,传送相关值。