Android 使用Post方式提交数据(登录)

  在Android中,提供了标准Java接口HttpURLConnection和Apache接口HttpClient,为客户端HTTP编程提供了丰富的支持。

  在HTTP通信中使用最多的就是GET和POST了,GET请求可以获取静态页面,也可以把参数放在URL字符串的后面,传递给服务器。POST与GET的不同之处在于POST的参数不是放在URL字符串里面,而是放在HTTP请求数据中。

  本文将使用标准Java接口HttpURLConnection,以一个实例演示如何使用POST方式向服务器提交数据,并将服务器的响应结果显示在Android客户端。

1.服务器端的准备

  为了完成该实例,我们需要在服务器端做以下准备工作:

  (1)我们需要在MyEclipse中创建一个Web工程,用来模拟服务器端的Web服务,这里,我将该工程命名为了“myhttp”。

  (2)修改该工程的“index.jsp”文件,添加两个输入框和一个提交按钮,作为该Web工程的显示页面。运行Tomcat,在浏览器中访问该Web工程,可以看到如图1所示的界面。

Android 使用Post方式提交数据(登录)

(3)在该Web工程中,创建一个继承自HttpServlet的LoginAction类,并实现其中的doPost()方法,用来响应图1所示页面的用户操作。具体实现如下:

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter(); String username = request.getParameter("username");
String password = request.getParameter("password"); //判断用户名密码是否正确
if(username.equals("admin") && password.equals("")) {
out.print("Login succeeded!");
}else {
out.print("Login failed!");
} out.flush();
out.close();
}

 由上述代码可以看出,当我们在图1所示的页面输入用户名“admin”,密码“123”时,点击提交按钮,会得到“Login succeeded!”的提示信息,如图2所示。若用户名、密码错误,则会得到“Login failed!”的提示信息。

Android 使用Post方式提交数据(登录)

至此,服务器端的准备工作就全部完成了。

2.客户端实现

  在Android客户端,我们需要完成的工作是:以POST方式发送用户名密码到上述服务器,并获得服务器的验证信息。

  我们分以下几个步骤来完成。

2.1 UI界面

  在Android工程中,我们需要完成一个简单的UI界面,用来完成用户名密码的输入、发送POST请求、显示服务器的验证结果,完成后的界面如图3所示。

Android 使用Post方式提交数据(登录)

在MainActivity中,我们需要获取两个EditText控件的输入,“提交”按键的监听,以及服务器验证结果的TextView内容显示。具体实现代码如下:

/*
* Function : 点击事件响应
* Author : 博客园-依旧淡然
*/
public void onClick(View view) {
switch(view.getId()) {
case R.id.button_submit:
String username = mEditText_userName.getText().toString();
String password = mEditText_password.getText().toString();
Map<String, String> params = new HashMap<String, String>();
params.put("username", username);
params.put("password", password);
mTextView_result.setText(HttpUtils.submitPostData(params, "utf-8"));
break;
}
}

2.2发送POST请求到服务器

  可以看到上述代码中,我们调用了HttpUtils类的静态方法submitPostData()完成了发送POST请求到服务器,并将该方法的返回值(服务器的响应结果)显示在了TextView控件中。

  在HttpUtils类中,submitPostData()方法的具体实现如下:

/*
* Function : 发送Post请求到服务器
* Param : params请求体内容,encode编码格式
* Author : 博客园-依旧淡然
*/
public static String submitPostData(Map<String, String> params, String encode) { byte[] data = getRequestData(params, encode).toString().getBytes();    //获得请求体
try {
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setConnectTimeout();   //设置连接超时时间
httpURLConnection.setDoInput(true); //打开输入流,以便从服务器获取数据
httpURLConnection.setDoOutput(true); //打开输出流,以便向服务器提交数据
httpURLConnection.setRequestMethod("POST");     //设置以Post方式提交数据
httpURLConnection.setUseCaches(false); //使用Post方式不能使用缓存
//设置请求体的类型是文本类型
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//设置请求体的长度
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));
//获得输出流,向服务器写入数据
OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(data); int response = httpURLConnection.getResponseCode(); //获得服务器的响应码
if(response == HttpURLConnection.HTTP_OK) {
InputStream inptStream = httpURLConnection.getInputStream();
return dealResponseResult(inptStream); //处理服务器的响应结果
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
}

通过以上的代码可以看出,在该方法中,其实完成了3件事:

  (1)将用户名密码封装成请求体,这是通过调用getRequestData()方法来实现的(后面会讲到这个方法的具体实现)。

  (2)设置HttpURLConnection对象的各种参数(其实是设置 HTTP协议请求体的各项参数),然后通过httpURLConnection.getOutputStream()方法获得服务器输出流 outputStream,再使用outputStream.write()方法将请求体内容发送给服务器。

  (3)判断服务器的响应码,通过httpURLConnection.getInputStream()方法获得服务器的响应输入流,然后再调用dealResponseResult()方法处理服务器的响应结果。

2.3封装请求体

  使用POST请求时,POST的参数不是放在URL字符串里,而是放在HTTP请求数据中,所以我们需要对POST的参数进行封装。

  针对该实例而言,我们发送的URL请求 是:http://192.168.1.101:8080/myhttp/servlet/LoginAction,但是我们需要将POST的参数(也就 是username和password)封装到该请求中,形成如下的形式:http://192.168.1.101:8080/myhttp /servlet/LoginAction?username=admin&password=123。我们该怎么做呢?如下的代码给出了一种实 现的方案:

/*
* Function : 封装请求体信息
* Param : params请求体内容,encode编码格式
* Author : 博客园-依旧淡然
*/
public static StringBuffer getRequestData(Map<String, String> params, String encode) {
StringBuffer stringBuffer = new StringBuffer(); //存储封装好的请求体信息
try {
for(Map.Entry<String, String> entry : params.entrySet()) {
stringBuffer.append(entry.getKey())
.append("=")
.append(URLEncoder.encode(entry.getValue(), encode))
.append("&");
}
stringBuffer.deleteCharAt(stringBuffer.length() - ); //删除最后的一个"&"
} catch (Exception e) {
e.printStackTrace();
}
return stringBuffer;
}
/*
* Function : 处理服务器的响应结果(将输入流转化成字符串)
* Param : inputStream服务器的响应输入流
* Author : 博客园-依旧淡然
*/
public static String dealResponseResult(InputStream inputStream) {
String resultData = null; //存储处理结果
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] data = new byte[];
int len = ;
try {
while((len = inputStream.read(data)) != -) {
byteArrayOutputStream.write(data, , len);
}
} catch (IOException e) {
e.printStackTrace();
}
resultData = new String(byteArrayOutputStream.toByteArray());
return resultData;
}

2.5运行效果

  最后,看看该实例的运行效果吧,如图4所示

Android 使用Post方式提交数据(登录)

上一篇:Android(java)学习笔记209:采用get请求提交数据到服务器(qq登录案例)


下一篇:python3 安装scrapy