首先我们先来 写服务器,这里就用登录来演示效果
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<body>
<form action="loginResult.xhtml" method="post">
Uname:<input type="text" name="uname"/><br/>
Upass:<input type="text" name="upass"/><br/>
<input type="submit" value="登录"/><br/>
form>
body>
body>
html>
LoginController
@RequestMapping("loginResult.xhtml")
public String loginResult(HttpServletRequest request){
String name = request.getParameter("uname");
String pass = request.getParameter("upass");
String result=null;
//判断数据库
if("admin".equals(name)&&"123456".equals(pass)){
result="success";
}else{
result="fail";
}
request.setAttribute("result", result);
return "loginResult";
}
@RequestMapping("login.xhtml")
public String login(HttpServletRequest request){
return "login";
}
loginResult.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title heretitle>
head>
<body>
${result }
body>
html>
为了节约时间就不跑数据库了,用户密码直接写死
这里输入用户名admin,密码123456,返回的结果是success,错误则返回fail。
服务器写好了,接下来我们要在客户端(安卓)向服务器(WEB)提交用户名和密码,然后返回对应的结果
直接上代码,先是布局文件
activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.zy.commitdatatoserver.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="admin"
android:id="@+id/et_main_uname"
/>
<EditText
android:id="@+id/et_main_upass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123456" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loginByGet"
android:text="登录(GET)" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="loginByPost"
android:text="登录(POST)" />
LinearLayout>
我们是向服务器提交数据,所以需要联网的权限。
<uses-permission android:name="android.permission.INTERNET">uses-permission>
MainActivity
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private EditText et_main_uname;
private EditText et_main_upass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_uname = (EditText) findViewById(R.id.et_main_uname);
et_main_upass = (EditText) findViewById(R.id.et_main_upass);
}
public void loginByGet(View view) {
String uname = et_main_uname.getText().toString();
String upass = et_main_upass.getText().toString();
String path = "http://192.168.42.253:8090/front/loginResult.xhtml";
//可变数组
new MyTask().execute(uname, upass, path, "GET");
}
public void loginByPost(View view) {
String uname = et_main_uname.getText().toString();
String upass = et_main_upass.getText().toString();
String path = "http://192.168.42.253:8090/front/loginResult.xhtml";
//可变数组
new MyTask().execute(uname, upass, path, "POST");
}
class MyTask extends AsyncTask {
private HttpURLConnection connection;
private URL url;
@Override
protected Object doInBackground(Object[] objects) {
//获取参数的值
String uname = objects[0].toString();
String upass = objects[1].toString();
String path = objects[2].toString();
String type = objects[3].toString();
String str="uname="+uname+"&upass="+upass;
try {
if ("GET".equals(type)) {
//用GET方式提交
path = path + "?"+str;
url = new URL(path);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(type);
} else if ("POST".equals(type)) {
//用POST方式提交
url = new URL(path);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(type);
//设置contentType contentLength
connection.setRequestProperty("Content-Length",str.length()+"");
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//设置允许对外输出数据
connection.setDoOutput(true);
//将用户名和密码提交到服务器
connection.getOutputStream().write(str.getBytes());
}
connection.setConnectTimeout(5000);
if (connection.getResponseCode() == 200) {
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String result = br.readLine();
return result;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
String s = (String) o;
Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
}
}
}
GET和POST的提交方式最大的区别就在于GET是地址栏传参,而POST需要加入
//设置contentType contentLength
connection.setRequestProperty(“Content-Length”,str.length()+”“);
connection.setRequestProperty(“Content-Type”,”application/x-www-form-urlencoded”);
这两行代码来设置内容类型与长度