一。客户端
1.请求处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
public class Utils { private static String uriLogin = "http://10.0.2.2:8080/ForAndroid/servlets/WatchLogin"; /** * 使用post的方式登录 */ public static String loginOfPost(String userName, String password) { HttpClient client = null; try { // 1.定义post请求的参数 List<NameValuePair> parameters = new ArrayList<NameValuePair>(); parameters.add(new BasicNameValuePair("username", userName)); parameters.add(new BasicNameValuePair("password", password)); // 把post请求的参数包装了一层. // 不写编码名称服务器收数据时乱码. 需要指定字符集为utf-8 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters, "utf-8"); // 2.定义post方法 HttpPost post = new HttpPost(uriLogin); // 设置请求头消息 //post.addHeader("Content-Length", "20"); // 3.设置参数. post.setEntity(entity); // 4.定义一个客户端 client = new DefaultHttpClient(); // 5.执行post方法, 返回一个HttpResponse对象,包含了状态信息和服务器返回的数据 HttpResponse response = client.execute(post); // 6.使用响应对象, 获得状态码, 处理内容 int statusCode = response.getStatusLine().getStatusCode(); // 获得状态码 if(statusCode == 200) { // 使用响应对象获得实体, 获得输入流 InputStream is = response.getEntity().getContent(); String text = getStringFromInputStream(is); return text; } else { System.err.println("请求失败: " + statusCode); } } catch (Exception e) { e.printStackTrace(); } finally { if(client != null) { client.getConnectionManager().shutdown(); // 关闭连接和释放资源 } } return null; } /** * 使用get的方式登录 */ public static String loginOfGet(String userName, String password) { HttpClient client = null; try { // 1.定义请求参数 String data = "username=" + userName + "&password=" + password; // 2.定义一个get请求方法 HttpGet get = new HttpGet(uriLogin + "?" + data); // 3.定义一个客户端 client = new DefaultHttpClient(); // 4.执行, 请求网络。 // response 服务器相应对象, 其中包含了状态信息和服务器返回的数据 HttpResponse response = client.execute(get); // 5.获得响应码,处理数据 int statusCode = response.getStatusLine().getStatusCode(); if(statusCode == 200) { InputStream is = response.getEntity().getContent(); String text = getStringFromInputStream(is); return text; } else { System.err.println("请求失败: " + statusCode); } } catch (Exception e) { e.printStackTrace(); } finally { if(client != null) { // 关闭连接, 和释放资源 client.getConnectionManager().shutdown(); } } return null; } /** * 根据流返回一个字符串信息 */ private static String getStringFromInputStream(InputStream is) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while((len = is.read(buffer)) != -1) { baos.write(buffer, 0, len); } is.close(); String html = baos.toString(); // 把流中的数据转换成字符串, 采用的编码是: utf-8 baos.close(); return html; } } |
2.主程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
public class MainActivity extends ActionBarActivity { private EditText etUserName; private EditText etPassword; private String userName; // 用户名 private String password; // 密码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etUserName = (EditText) findViewById(R.id.et_username); etPassword = (EditText) findViewById(R.id.et_password); userName = etUserName.getText().toString(); password = etPassword.getText().toString(); } /** * 使用httpClient方式提交get请求 */ public void doHttpClientOfGet(View v) { new Thread(new Runnable() { public void run() { // 请求网络 final String state = Utils.loginOfGet(userName, password); // 执行任务在主线程中 runOnUiThread(new Runnable() { @Override public void run() { // 就是在主线程中操作 Toast.makeText(MainActivity.this, state, 0).show(); } }); } }).start(); } /** * 使用httpClient方式提交post请求 */ public void doHttpClientOfPost(View v) { new Thread(new Runnable() { @Override public void run() { final String state = Utils.loginOfPost(userName, password); // 执行任务在主线程中 runOnUiThread(new Runnable() { @Override public void run() { // 就是在主线程中操作 Toast.makeText(MainActivity.this, state, 0).show(); } }); } }).start(); } } |
3.布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<LinearLayout 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:orientation="vertical" tools:context=".MainActivity" > <EditText android:id="@+id/et_username" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="姓名vigiles@qq.com" /> <EditText android:id="@+id/et_password" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="密码q09w486t0=cxz" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doHttpClientOfGet" android:text="使用HttpClient方式提交Get请求" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doHttpClientOfPost" android:text="使用HttpClient方式提交Post请求" /> </LinearLayout> |
4.权限
1 2 3 |
<uses-permission android:name="android.permission.INTERNET"/> |
二。服务端
1.请求处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public class WatchLogin extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); String name = request.getParameter("username"); String pswd = request.getParameter("password"); // GET访问时,须转码 name = new String(name.getBytes("iso8859-1"), "utf-8"); pswd = new String(pswd.getBytes("iso8859-1"), "utf-8"); System.out.println("用户:" +name + "\t密码:" + pswd); // 默认使用iso8859-1编码输出,如果码表没有输出的字符,则使用当前系统编码GBK。指定为utf-8 response.getOutputStream().write(("GET反馈:你的名字:" + name + ", 密码:" + pswd).getBytes("utf-8")); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); String name = request.getParameter("username"); String pswd = request.getParameter("password"); System.out.println("用户:" +name + "\t密码:" + pswd); // 默认使用iso8859-1编码输出,如果码表没有输出的字符,则使用当前系统编码GBK。指定为utf-8 response.getOutputStream().write(("POST反馈:你的名字:" + name + ", 密码:" + pswd).getBytes("utf-8")); } } |
2.请求路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>WatchLogin</servlet-name> <servlet-class>com.cuiweiyou.servlets.WatchLogin</servlet-class> </servlet> <servlet-mapping> <servlet-name>WatchLogin</servlet-name> <url-pattern>/servlets/WatchLogin</url-pattern> </servlet-mapping> </web-app> |
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/193.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设