接着上篇《Android 采用get方式提交数据到服务器》,本文来实现采用post方式提交数据到服务器
首先对比一下get方式和post方式:
修改布局:
<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_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" android:inputType="text" /> <EditText android:id="@+id/et_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:inputType="textPassword" /> <Button android:onClick="LoginByGet" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="GET方式登录" /> <Button android:onClick="LoginByPost" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="POST方式登录" /> </LinearLayout>
添加代码:
package com.wuyudong.loginclient; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.os.Build; import android.os.Bundle; import android.os.StrictMode; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText et_name; private EditText et_pwd; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_name = (EditText) findViewById(R.id.et_name); et_pwd = (EditText) findViewById(R.id.et_pwd); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() .permitAll().build(); StrictMode.setThreadPolicy(policy); } public void LoginByGet(View view) { String name = et_name.getText().toString().trim(); String pwd = et_pwd.getText().toString().trim(); if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) { Toast.makeText(this, "用户名密码不能为空", 0).show(); } else { // 模拟http请求,提交数据到服务器 String path = "http://169.254.168.71:8080/web/LoginServlet?username=" + name + "&password=" + pwd; try { URL url = new URL(path); // 2.建立一个http连接 HttpURLConnection conn = (HttpURLConnection) url .openConnection(); // 3.设置一些请求方式 conn.setRequestMethod("GET");// 注意GET单词字幕一定要大写 conn.setRequestProperty( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"); int code = conn.getResponseCode(); // 服务器的响应码 200 OK //404 页面找不到 // // 503服务器内部错误 if (code == 200) { InputStream is = conn.getInputStream(); // 把is的内容转换为字符串 ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = is.read(buffer)) != -1) { bos.write(buffer, 0, len); } String result = new String(bos.toByteArray()); is.close(); Toast.makeText(this, result, 0).show(); } else { Toast.makeText(this, "请求失败,失败原因: " + code, 0).show(); } } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "请求失败,请检查logcat日志控制台", 0).show(); } } } /** * 采用post的方式提交数据到服务器 * * @param view */ public void LoginByPost(View view) { String name = et_name.getText().toString().trim(); String pwd = et_pwd.getText().toString().trim(); if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) { Toast.makeText(this, "用户名密码不能为空", 0).show(); } else { try { String path = "http://169.254.168.71:8080/web/LoginServlet?username=" + name + "&password=" + pwd; // 1.定义请求url URL url = new URL(path); // 2.建立一个http的连接 HttpURLConnection conn = (HttpURLConnection) url .openConnection(); // 3.设置一些请求的参数 conn.setRequestMethod("POST"); conn.setRequestProperty( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); String data = "username=" + name + "&password=" + pwd; conn.setRequestProperty("Content-Length", data.length() + ""); conn.setConnectTimeout(5000);//设置连接超时时间 conn.setReadTimeout(5000); //设置读取的超时时间 // 4.一定要记得设置 把数据以流的方式写给服务器 conn.setDoOutput(true); // 设置要向服务器写数据 conn.getOutputStream().write(data.getBytes()); int code = conn.getResponseCode(); // 服务器的响应码 200 OK //404 页面找不到 // // 503服务器内部错误 if (code == 200) { InputStream is = conn.getInputStream(); // 把is的内容转换为字符串 ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while ((len = is.read(buffer)) != -1) { bos.write(buffer, 0, len); } String result = new String(bos.toByteArray()); is.close(); Toast.makeText(this, result, 0).show(); } else { Toast.makeText(this, "请求失败,失败原因: " + code, 0).show(); } } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "请求失败,请检查logcat日志控制台", 0).show(); } } } }