一,在安卓上打印信息
Toast.makeText(LoginActivity.this, xinxi, 1000).show();
二.跳转页面
//实例化intent
Intent intent = new Intent();
//使用intentsetClass设置起跳页面,跳转后的页面
intent.setClass(LoginActivity.this,IndexActivity.class);
//起跳方法,参数放intent
LoginActivity.this.startActivity(intent);
注:使用Activity,该类需要继承Activity,并重写onCreate()方法
三.Activity类中绑定xml页面
super.setContentView(R.layout.login);
四.Activity类中绑定button控件
button =(Button) this.findViewById(R.id.button1);
五.Activity类中对控件绑定监听类
button.setOnClickListener(new MyListener());
六.后台代码存值跳转到前台代码
//在后台线程中实例化message
Message message = new Message();
//实例化bundle
Bundle bundle = new Bundle();
//把需要存入的值放入bundle
bundle.putString("neirong1", neirong);
//把bundle放入message
message.setData(bundle);
//使用当前类handler.sendMessage放入message
LoginActivity.this.handler.sendMessage(message);
七.前台获取后台存入的值
//通过msg参数获得bundle
Bundle bundle = msg.getData();
//通过bundle拿到之前存值的键
String xinxi = bundle.getString("neirong1");
//在安卓上打印信息
Toast.makeText(LoginActivity.this, xinxi, 1000).show();
例子:使用绑定事件启动监听类,监听类启动后台线程,后台线程调用前台线程
安卓端代码:
public class LoginActivity extends Activity{
private Button button;
private EditText name;
private EditText pwd;
private MyHandle handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//绑定xml+Activity
super.setContentView(R.layout.login);
//绑定button
button =(Button) this.findViewById(R.id.button1);
button.setOnClickListener(new MyListener());
//实例化handier
handler = new MyHandle();
}
//监听类代码
class MyListener implements OnClickListener{
@Override
public void onClick(View arg0) {
//在监听类中实例化后台线程并开启
Mythread thread = new Mythread();
thread.start();
}
}
//后台线程代码
class Mythread extends Thread{
@Override
public void run() {
int data = 0;
String neirong= "";
//绑定控件信息
name=(EditText)findViewById(R.id.name1);
pwd=(EditText)findViewById(R.id.pwd1);
try {
//使用URL地址访问服务器
URL url = new URL(
"http://192.168.1.107:8080/Zhenai/servlet/LoginServlet");
//通过URL获取到connection
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
//可以设置Post提交方式
/* connection.setRequestMethod("post");
* 可以设置超时时间
connection.setConnectTimeout(5000);*/
//设置默认允许输入和输出
connection.setDoInput(true);
connection.setDoOutput(true);
//通过connection得到输出流
OutputStream os = connection.getOutputStream();
//通过输出流输出相应的信息
os.write(("name="+name.getText().toString()+"&pwd="+pwd.getText().toString()+"&biaoji=android").getBytes());
//200为正常,进行读取服务器传过来的信息
if (connection.getResponseCode() == 200) {
InputStream is = connection.getInputStream();
while ((data=is.read())!= -1) {
neirong += (char) data;
}
}
} catch (Exception e) {
// TODO: handle exception
}
//得到服务器传过来的信息后进行判断跳转
if (neirong.equals("SUCCESS")) {
Intent intent = new Intent();
intent.setClass(LoginActivity.this,IndexActivity.class);
LoginActivity.this.startActivity(intent);
}
System.out.println(neirong);
//在后台线程中实例化message
Message message = new Message();
//实例化bundle
Bundle bundle = new Bundle();
//把需要存入的值放入bundle
bundle.putString("neirong1", neirong);
//把bundle放入message
message.setData(bundle);
//使用当前类handler.sendMessage放入message
LoginActivity.this.handler.sendMessage(message);
}
}
//前台线程代码
class MyHandle extends Handler{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
//通过msg参数获得bundle
Bundle bundle = msg.getData();
//通过bundle拿到之前存值的键
String xinxi = bundle.getString("neirong1");
//在安卓上打印信息
Toast.makeText(LoginActivity.this, xinxi, 1000).show();
}
}
}
服务器代码使用Servlet编写:
public class LoginServlet extends HttpServlet {
public LoginServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//设置编码格式
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//获取安卓端控件的内容
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
String bj = request.getParameter("biaoji");
//测试输出一下是否得到值
System.out.println(name+pwd+bj);
//判断是通过jsp还是安卓端进入
if (bj.equals("jsp")) {
//如是jsp端并且名字和密码是否正确,则跳转到jsp页面
if (name.equals("admin")&&pwd.equals("123456")) {
response.sendRedirect("/Zhenai/index.jsp");
}else {
response.sendRedirect("/Zhenai/login.jsp");
}
}else if (bj.equals("android")) {
//如是安卓端判断名字和密码是否正确,则输出对应的信息
if (name.equals("admin")&&pwd.equals("123456")) {
out.print("SUCCESS");
}else {
out.print("ERROR");
out.print(name+pwd);
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//dopost使用doget方法
doGet(request, response);
}
public void init() throws ServletException {
// Put your code here
}
}