使用HttpURLConnection采用Post方式请求数据
2017-01-14 14:22
513 查看
1. 服务端
doPost(){
doGet(request,response);
}
2. Post方式不在URL后面加数据,而是用流的方式传递;GET在URL后传输数据
3. 是否传递---请求头:setRequestProperty();
MainActivity中:
[java] view
plain copy
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();
}
loginOfPost方法:
[java] view
plain copy
/**
* 使用post的方式登录
* @param userName
* @param password
* @return
*/
public static String loginOfPost(String userName, String password) {
HttpURLConnection conn = null;
try {
URL url = new URL("http://10.0.2.2:8080/ServerItheima28/servlet/LoginServlet");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(10000); // 连接的超时时间
conn.setReadTimeout(5000); // 读数据的超时时间
conn.setDoOutput(true); // 必须设置此方法, 允许输出****** 默认情况下, 系统不允许向服务器输出内容
/ conn.setRequestProperty("Content-Length", 234); // 设置请求头消息, 可以设置多个
// post请求的参数
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;
}
根据流返回一个字符串信息;
[java] view
plain copy
/**
* 根据流返回一个字符串信息
* @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");
baos.close();
return html;
}
doPost(){
doGet(request,response);
}
2. Post方式不在URL后面加数据,而是用流的方式传递;GET在URL后传输数据
3. 是否传递---请求头:setRequestProperty();
MainActivity中:
[java] view
plain copy
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();
}
loginOfPost方法:
[java] view
plain copy
/**
* 使用post的方式登录
* @param userName
* @param password
* @return
*/
public static String loginOfPost(String userName, String password) {
HttpURLConnection conn = null;
try {
URL url = new URL("http://10.0.2.2:8080/ServerItheima28/servlet/LoginServlet");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(10000); // 连接的超时时间
conn.setReadTimeout(5000); // 读数据的超时时间
conn.setDoOutput(true); // 必须设置此方法, 允许输出****** 默认情况下, 系统不允许向服务器输出内容
/ conn.setRequestProperty("Content-Length", 234); // 设置请求头消息, 可以设置多个
// post请求的参数
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;
}
根据流返回一个字符串信息;
[java] view
plain copy
/**
* 根据流返回一个字符串信息
* @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");
baos.close();
return html;
}
相关文章推荐
- 使用HttpURLConnection和使用HttpClient方式请求网络采用get方式和post方式请求数据
- 【黑马Android】(05)短信/查询和添加/内容观察者使用/子线程网络图片查看器和Handler消息处理器/html查看器/使用HttpURLConnection采用Post方式请求数据/开源项目
- 使用HttpURLConnection采用Post方式请求数据
- 使用HttpURLConnection采用get方式或post方式请求数据
- 使用HttpURLConnection通过POST方式提交请求数据,并上传文件
- 使用HttpURLConnection采用get方式请求数据-----乱码问题
- HttpURLconnection使用POST方式提交JSON数据给服务器
- Android使用HttpURLConnection进行POST请求,向服务器上传数据
- HttpUrlConnection两种请求数据的方式get和post提交数据
- 第十二篇:Android GET请求获取网络数据以及POST数据(使用HttpURLConnection)
- android(21)(使用HttpURLConnection,HttpClient和AsyncHttpClient分别实现post和get请求数据)
- HttpURLconnection使用POST方式提交JSON数据给服务器
- 使用HttpURLConnection通过GET、POST请求数据
- 使用HttpURLConnection开发自动发送Get、Post请求并获取响应
- Http学习之使用HttpURLConnection发送post请求深入
- 使用HttpURLConnection进行POST方式提交
- HttpURL连接远程服务器Get和Post方式请求并返回数据
- nodejs的http.request使用post方式提交数据请求
- Http学习之使用HttpURLConnection发送post请求深入
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片