服务器(Servlet):
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Second extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String name=req.getParameter("userName");
String password=req.getParameter("passWord");
System.out.println("用户名:"+name);
System.out.println("密码:"+password);
OutputStream out=resp.getOutputStream();
if(("admin".equals(name))&&("123".equals(password))){
out.write("ok".getBytes());
}
else {
out.write("no".getBytes());
}
}
}
客户端:
连接工具类:
package util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.R.integer;
import android.app.Activity;
import android.util.Log;
import android.widget.Toast;
public class ServletConDemo{
//URL地址
private String urlName = "http://10.0.2.2:8080/ServletDemo/Login";
public String httpGetConnection(String userName,String password) {
Log.i("action", "开始工作");
//GET 方式 : get方式请求参数跟在http://10.0.2.2:8080/ServletDemo/Login?username=aaa&password=111
String para = "?userName=" + userName + "&passWord=" + password;
//输入流
StringBuilder sb=new StringBuilder();
BufferedReader br = null;
URL url;
try {
url = new URL(urlName+para);
//创建对象
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
//
connection.setConnectTimeout(5000);
//
connection.setRequestMethod("GET");
//获取服务器响应代码
/*
* 404:找不到资源
* 500:服务器程序出错
* 200:ok
*/
if (connection.getResponseCode() == 200) {
Log.i("connection", "连接成功");
//获取响应内容
InputStream in = connection.getInputStream();
br = new BufferedReader(new InputStreamReader(in, "utf-8"));
String line=null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
return sb.toString();
}
else {
Log.i("connection——no", "连接不成功");
}
} catch (Exception e) {
// TODO Auto-generated catch block
Log.i("err", "有异常");
}
return null;
}
}
activity:
package com.example.servlettestdemo;
import java.net.URL;
import util.ServletConDemo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView showTextView;
private Button loginButton;
private EditText userNameEditText, pwdEditText;
ServletConDemo servletConnection;
private String userName, password;
private Handler handler;
// 授权访问网络<uses-permission/>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findId();
//处理线程发送的消息
handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 1) {
showTextView.setText("登录成功了");
}
if (msg.what == 0) {
showTextView.setText("登录失败了");
}
};
};
}
/*
* 点击登录按钮时实现该方法
*/
public void click_login(View source) {
userName = userNameEditText.getText().toString();
password = pwdEditText.getText().toString();
new Thread() {
public void run() {
servletConnection = new ServletConDemo();
String result = servletConnection.httpGetConnection(userName,
password);
if ("ok".equals(result)) {
Log.i("result", result);
handler.sendEmptyMessage(1);
} else {
Log.i("result", result);
handler.sendEmptyMessage(0);
}
};
}.start();
}
public void findId() {
loginButton = (Button) findViewById(R.id.button_login);
userNameEditText = (EditText) findViewById(R.id.editText_user);
pwdEditText = (EditText) findViewById(R.id.editText_password);
showTextView = (TextView) findViewById(R.id.textView_show);
}
}
界面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="24dp"
android:layout_marginTop="60dp"
android:text="用户名:" />
<TextView
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/user"
android:layout_below="@+id/user"
android:layout_marginTop="37dp"
android:text="密码:" />
<EditText
android:id="@+id/editText_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/user"
android:layout_toRightOf="@+id/user"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/password"
android:layout_alignBottom="@+id/password"
android:layout_alignParentRight="true"
android:ems="10" />
<Button
android:id="@+id/button_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText_password"
android:layout_below="@+id/editText_password"
android:layout_marginTop="24dp"
android:onClick="click_login"
android:text="登录" />
<TextView
android:id="@+id/textView_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button_login"
android:layout_marginTop="86dp"
android:layout_toLeftOf="@+id/button_login"
android:text="服务器返回结果"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>