MainActivity中:
package com.zhidi.httppost;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText et_username;
private EditText et_password;
private Button bt_show;
String result="";
/**post请求URL地址*/
private static final String URL="http://192.168.2.3:8080/test/servlet/LoginServlet";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
bt_show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//android4.0后的新的特性,网络数据请求时不能放在主线程中。
//9、使用线程执行访问服务器,获取返回信息后通知主线程更新UI或者提示信息。
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 1) {
//8、提示读取结果
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
}
};
// 启动线程来执行任务
new Thread() {
public void run() {
//请求网络
loginByPost(et_username.getText().toString(), et_password.getText().toString());
Message m = new Message();
m.what = 1;
// 发送消息到Handler
handler.sendMessage(m);
}
}.start();
}
});
}
private void init(){
et_username=(EditText)findViewById(R.id.et_username);
et_password=(EditText)findViewById(R.id.et_password);
bt_show=(Button)findViewById(R.id.bt_login);
}
/**
* 请求后台,判断登录结果。
* @param username
* @param password
*/
public void loginByPost(String username, String password) {
try {
//1、根据地址创建URL对象(网络访问的url)
URL url = new URL(URL);
//2、url.openConnection()打开网络链接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//3、设置请求的方式
conn.setRequestMethod("POST");
conn.setDoInput(true);//发送POST请求必须设置允许输出
conn.setDoOutput(true);//发送POST请求必须设置允许输入
//4、设置请求的头
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Charset", "utf-8");
String data = "username=" + URLEncoder.encode(username, "UTF-8")
+ "&password=" + URLEncoder.encode(password, "UTF-8");//传递的数据
conn.setRequestProperty("Content-Length",String.valueOf(data.getBytes().length));
//5、获取输出流
OutputStream os = conn.getOutputStream();
os.write(data.getBytes());
os.flush();
//6、获取响应的输入流对象
InputStreamReader is = new InputStreamReader(conn.getInputStream());
BufferedReader bufferedReader = new BufferedReader(is);
StringBuffer strBuffer = new StringBuffer();
String line = null;
//7、读取服务器返回信息
while ((line = bufferedReader.readLine()) != null) {
strBuffer.append(line);
}
result = strBuffer.toString();
//8、关闭InputStream、关闭http连接
is.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
AndroidManifest中注册网络权限
<uses-permission android:name="android.permission.INTERNET"/>