安装软件:点击打开链接
大体流程:点击打开链接(已读)
点击打开链接(未读)
暂时使用:android客户端:疯狂android讲义第19章AuctionClient里 (Login.java//MainActivity.java//HomeListener.java//callbacks.java及util里DialogUtil、HttpUtil)
可能初学遇到问题:
URL
*必许给app增添访问网络的权限:
AndroidManifest.xml里加权限: <uses-permission android:name="android.permission.INTERNET"/>
代码 Login.java:
package com.example.administrator.bs01;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.administrator.bs01.util.DialogUtil;
import com.example.administrator.bs01.util.HttpUtil;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class Login extends Activity
{
// 定义界面中两个文本框
EditText etName, etPass;
// 定义界面中两个按钮
Button bnLogin, bnCancel;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// 获取界面中两个编辑框
etName = (EditText) findViewById(R.id.userEditText);
etPass = (EditText) findViewById(R.id.pwdEditText);
// 获取界面中的两个按钮
bnLogin = (Button) findViewById(R.id.bnLogin);
bnCancel = (Button) findViewById(R.id.bnCancel);
// 为bnCancal按钮的单击事件绑定事件监听器
bnCancel.setOnClickListener(new HomeListener(this));
bnLogin.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// 执行输入校验
if (validate()) // ①
{
// 如果登录成功
if (loginPro()) // ②
{
// 启动Main Activity
Intent intent = new Intent(Login.this
, MainActivity.class);//更改
startActivity(intent);
// 结束该Activity
finish();
}
else
{
DialogUtil.showDialog(Login.this
, "用户名称或者密码错误,请重新输入!", false);
}
}
}
});
}
private boolean loginPro()
{
// 获取用户输入的用户名、密码
String username = etName.getText().toString();
String pwd = etPass.getText().toString();
JSONObject jsonObj;
try
{
jsonObj = query(username, pwd);
// 如果userId 大于0
if (jsonObj.getInt("userId")>0)
{
return true;
}
}
catch (Exception e)
{
DialogUtil.showDialog(this
, "服务器响应异常,请稍后再试!", false);
e.printStackTrace();
}
return false;
}
// 对用户输入的用户名、密码进行校验
private boolean validate()
{
String username = etName.getText().toString().trim();
if (username.equals(""))
{
DialogUtil.showDialog(this, "用户账户是必填项!", false);
return false;
}
String pwd = etPass.getText().toString().trim();
if (pwd.equals(""))
{
DialogUtil.showDialog(this, "用户口令是必填项!", false);
return false;
}
return true;
}
// 定义发送请求的方法
private JSONObject query(String username, String password)
throws Exception
{
// 使用Map封装请求参数
Map<String, String> map = new HashMap<>();
map.put("username", username);
map.put("password", password);
// 定义发送请求的URL
String url = HttpUtil.BASE_URL+ "login.jsp";根据个人服务器设置BASE_URL(在HttpUtil中更改设置)
// 发送请求
return new JSONObject(HttpUtil.postRequest(url, map));
}
}
服务器端:
package servlet;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
//import servlet.base.BaseServlet;
//import org.crazyit.auction.service.AuctionManager;
import java.io.*;
import org.json.*;
/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2011-2016, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
@WebServlet(urlPatterns="/android/login.jsp")
public class LoginServlet extends HttpServlet
{
public void service(HttpServletRequest request ,
HttpServletResponse response)
throws IOException , ServletException
{
String user = request.getParameter("username");
String pass = request.getParameter("password");
System.out.println(user + "-++-" + pass);
response.setContentType("text/html; charset=utf-8");
int userId=0;
// 登录成功
if (user.equals("success"))
{
userId = 1;
request.getSession(true).setAttribute("userId" , userId);
}
try
{
// 把验证的userId封装成JSONObject
JSONObject jsonObj = new JSONObject()
.put("userId" , userId);
// 输出响应
response.getWriter().println(jsonObj.toString());
}
catch (JSONException ex)
{
ex.printStackTrace();
}
}
}