前言:在Android开发的过程中,必须会接触到数据交互(访问数据,写入数据等你等),既然接触到数据的交互,那么自然而然就是使用通讯间的协议来进行请求,最常见的协议就是Http协议,Http协议包括两个具体的请求方式-Get以及Post。
public class LoginServer {
/**
*get的方式请求
*@param username 用户名
*@param password 密码
*@return 返回null 登录异常
*/
public static String loginByGet(String username,String password){
//get的方式提交就是url拼接的方式
String path = "http://172.16.168.111:1010/login.php?username="+username+"&password="+password;
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
//获得结果码
int responseCode = connection.getResponseCode();
if(responseCode ==200){
//请求成功 获得返回的流
InputStream is = connection.getInputStream();
return IOSUtil.inputStream2String(is);
}else {
//请求失败
return null;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/** * post的方式请求
*@param username 用户名
*@param password 密码
*@return 返回null 登录异常
*/
public static String loginByPost(String username,String password){
String path = "http://172.16.168.111:1010/login.php";
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setRequestMethod("POST");
//数据准备
String data = "username="+username+"&password="+password;
//至少要设置的两个请求头
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", data.length()+"");
//post的方式提交实际上是留的方式提交给服务器
connection.setDoOutput(true);
OutputStream outputStream = connection.getOutputStream();
outputStream.write(data.getBytes());
//获得结果码
int responseCode = connection.getResponseCode();
if(responseCode ==200){
//请求成功
InputStream is = connection.getInputStream();
return IOSUtil.inputStream2String(is);
}else {
//请求失败
return null;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}