使用HttpURLConnection请求数据,通过GET和POST两种方式实现登录功能。
GET请求和POST请求区别:请求参数在传递的过程中方式不同
GET提交——请求参数将会附加在请求的URL后面作为请求地址的一部分带到服务器端,可以传输的数据大小有限制一般不允许超过1K.
POST提交——请求参数将会在http请求的实体内容中进行传输,这种方式传输的数据没有大小限制.
NetUtils.class
/**GET方法登录
*
* @param userName
* @param password
* @return
*/
public static String loginOfGet(String userName, String password) {
HttpURLConnection conn = null;
try {
String data = "username=" + URLEncoder.encode(userName) + "&password=" +password;
URL url = new URL("" + data);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(10000);
conn.setReadTimeout(5000);
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
InputStream is = conn.getInputStream();
String state = getStringFromInputStream(is);
return state;
} else {
Log.i(TAG, "访问失败:"+responseCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return null;
}
/**POST方法登录
*
* @param userName
* @param password
* @return
*/
public static String loginOfPost(String userName, String password) {
HttpURLConnection conn = null;
try {
URL url = new URL("");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(10000);
conn.setReadTimeout(5000);
conn.setDoOutput(true); //允许输出(必须设置!)
//conn.setRequestProperty("Content-Length", "234"); //设置请求头消息,可以设置多个
String data = "username=" + userName + "&password=" + password;
//获得一个输出流,用于向服务器写数据,默认情况下,系统不允许向服务器输出内容
OutputStream out = conn.getOutputStream();
out.write(data.getBytes());
out.flush();
out.close();
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
InputStream is = conn.getInputStream();
String state = getStringFromInputStream(is);
return state;
} else {
Log.i(TAG, "访问失败:"+responseCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(conn != null) {
conn.disconnect();
}
}
return null;
}
/**
* 根据流返回一个字符串信息
* @param is
* @return
* @throws IOException
*/
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
String html = new String(baos.toByteArray(), "GBK"); //采用GBK编码将baos流转换为字符串
baos.close();
return html;
}
下面是UI中的操作:
MainActivity.class
public void doGet(View v) {
final String userName = etUserName.getText().toString();
final String password = etPassword.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
final String state = NetUtils.loginOfGet(userName, password);
//执行任务在主线程中
runOnUiThread(new Runnable() {
@Override
public void run() {
// 在主线程中操作
Toast.makeText(MainActivity.this, state, 0).show();
}
});
}
}).start();
}
public void doPost(View v) {
final String userName = etUserName.getText().toString();
final String password = etPassword.getText().toString();
new Thread(new Runnable() {
@Override
public void run() {
final String state = NetUtils.loginOfPost(userName, password);
//执行任务在主线程中
runOnUiThread(new Runnable() {
@Override
public void run() {
// 在主线程中操作
Toast.makeText(MainActivity.this, state, 0).show();
}
});
}
}).start();
}
从GET和POST两种方法的实现过程中,可看出两者的不同:
POST需要向服务器传递一个输出流,将客户端的信息data(name和password)传递给服务器;GET方式则不需执行此步骤,而是将data拼接在url地址后面。
需要注意:客户端默认状态下不允许向服务器传递输出流,必须使用conn.setDoOutput(true)方法进行设置。