转载请注明出处:http://blog.csdn.net/dennisxzr/article/details/23847073(来自Dennis锐少的csdn博客)
简单的使用jsp+sevlet+mysql来创建android的服务端
实现客户端与服务端的交互
可以在此基础上练习 添加其他功能
客户端代码:
- package com.android.cn;
- import org.apache.http.client.methods.HttpPost;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class AndroidDemosActivity extends Activity {
- private Button cancelBtn, loginBtn;
- private EditText userEditText, pwdEditText;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- cancelBtn = (Button) findViewById(R.id.Canclebtn);
- loginBtn = (Button) findViewById(R.id.Loginbtn);
- userEditText = (EditText) findViewById(R.id.etUserName);
- pwdEditText = (EditText) findViewById(R.id.etPassword);
- cancelBtn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- finish();
- }
- });
- loginBtn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- HttpPost request = new HttpPost("http://www.baidu.com");
- if (validate()) {
- if (login()) {
- Intent intent = new Intent(AndroidDemosActivity.this,
- MainActivity.class);
- startActivity(intent);
- } else {
- showDialog("用户名称或者密码错误,请重新输入!");
- }
- }
- }
- });
- }
- private boolean login() {
- String username = userEditText.getText().toString();
- String pwd = pwdEditText.getText().toString();
- String result = query(username, pwd);
- if (result != null && result.equals("1")) {
- return true;
- } else {
- return false;
- }
- }
- private boolean validate() {
- String username = userEditText.getText().toString();
- if (username.equals("") && username == null) {
- showDialog("用户名称是必填项!");
- return false;
- }
- String pwd = pwdEditText.getText().toString();
- if (pwd.equals("")) {
- showDialog("用户密码是必填项!");
- return false;
- }
- return true;
- }
- private void showDialog(String msg) {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setMessage(msg).setCancelable(false).setPositiveButton("确定",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- }
- });
- AlertDialog alert = builder.create();
- alert.show();
- }
- private String query(String username, String password) {
- String queryString = "username=" + username + "&password=" + password;
- String url = HttpUtil.BASE_URL + "servlet/ServiceTest?" + queryString;
- return HttpUtil.queryStringForGet(url);
- }
- }
- package com.server.an;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- *
- * Android客户端发来的请求
- */
- @SuppressWarnings("serial")
- public class ServiceTest extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- System.out.println("do Get");
- response.setContentType("text/html");
- response.setCharacterEncoding("utf-8");
- PrintWriter out = response.getWriter();
- UserDao dao = new UserDaoImpl();
- // 获得客户端请求参数
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- User u = dao.login(username, password);
- if (u != null) {
- // 响应客户端内容,登录成功
- //out.print("登录成功");
- out.print("登录成功");
- } else {
- // 响应客户端内容,登录失败
- out.print("登录失败");
- }
- out.flush();
- out.close();
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- System.out.println("do Post");
- String username = request.getParameter("username");
- PrintWriter out = response.getWriter();
- UserDao dao = new UserDaoImpl();
- // 获得客户端请求参数
- String password = request.getParameter("password");
- User u = dao.login(username, password);
- if (u != null) {
- // 响应客户端内容,登录成功
- //out.print("登录成功");
- response.sendRedirect("/AndroidServer/success.jsp");
- } else {
- // 响应客户端内容,登录失败
- out.print("登录失败");
- }
- out.flush();
- out.close();
- }
- public void init() throws ServletException {
- }
- public ServiceTest() {
- super();
- }
- public void destroy() {
- super.destroy();
- }
- }
转载请注明出处: http://blog.csdn.net/dennisxzr/article/details/23847073 (来自Dennis锐少的csdn博客)