HttpUrlConnection的post请求
来源:互联网 发布:苗千网络 编辑:程序博客网 时间:2024/06/05 16:07
new Thread(){
public void run() {
//?userName="+name+"&passWord="+pwd
String path="http://169.254.135.196:8080/LogServer/servlet/LogServlet";
String data ="userName="+name+"&passWord="+pwd;
//参数是请求的地址
try {
URL url = new URL(path);
//得到HttpURLConnection 对像
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置它的请求方式
connection.setRequestMethod("POST");
//设轩它的连接超时时间
connection.setConnectTimeout(5000);
//代表允客户端给服务器写内容
connection.setDoInput(true);
//把参数写给服务器
connection.getOutputStream().write(data.getBytes());
//得到服务器给客户端返回的状态码 200 304 307 404 500
int responseCode = connection.getResponseCode();
baos = new ByteArrayOutputStream();
//请求服务器成功
if(responseCode==200){
//得到服务器返回的流 其实就是数据
InputStream is = connection.getInputStream();
byte[] arr = new byte[1024];
int len;
while((len=is.read(arr))!=-1){
//baos.write(arr);
baos.write(arr, 0, len);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* 在子线程里 不能更新ui和toast
*/
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, baos.toString(), 0).show();
}
});
};
}.start();
public void run() {
//?userName="+name+"&passWord="+pwd
String path="http://169.254.135.196:8080/LogServer/servlet/LogServlet";
String data ="userName="+name+"&passWord="+pwd;
//参数是请求的地址
try {
URL url = new URL(path);
//得到HttpURLConnection 对像
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置它的请求方式
connection.setRequestMethod("POST");
//设轩它的连接超时时间
connection.setConnectTimeout(5000);
//代表允客户端给服务器写内容
connection.setDoInput(true);
//把参数写给服务器
connection.getOutputStream().write(data.getBytes());
//得到服务器给客户端返回的状态码 200 304 307 404 500
int responseCode = connection.getResponseCode();
baos = new ByteArrayOutputStream();
//请求服务器成功
if(responseCode==200){
//得到服务器返回的流 其实就是数据
InputStream is = connection.getInputStream();
byte[] arr = new byte[1024];
int len;
while((len=is.read(arr))!=-1){
//baos.write(arr);
baos.write(arr, 0, len);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* 在子线程里 不能更新ui和toast
*/
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, baos.toString(), 0).show();
}
});
};
}.start();
阅读全文
0 0
- HttpURLConnection的POST请求
- HttpUrlConnection的post请求
- HttpURLConnection--HttpURLConnection的Post请求方式
- HttpURLConnection的post请求总结
- HttpURLConnection发出的post请求
- HttpURLConnection发出的post请求
- HttpURLConnection的Post请求方式
- HttpUrlConnection的GET请求和POST请求
- httpurlconnection的Get请求和Post请求
- HttpURLConnection 的get跟post请求
- HttpURLConnection的get和post请求
- HttpURLConnection的GET和POST请求
- HttpURLConnection的get和post请求实例
- 【Java】HttpURLConnection,HttpClient 的 Get,Post 请求
- HttpURLConnection的POST请求加载数据图片
- HttpUrlConnection的get和post请求方法
- HttpURLConnection的GET和POST请求方式
- 可靠的HttpUrlConnection实现get/post请求
- jquery实现点击某元素之外触发事件
- 前后端分离开发的优缺点
- Guava文件操作
- Canvas能做什么?
- 三方登录友盟 sso授权集成微信、QQ、微博方法,及各个平台注册
- HttpUrlConnection的post请求
- Unity入门操作_ 生命周期函数_018
- 【java面试系列之操作系统】进程和线程、死锁、内存管理方式
- http协议
- Python
- 关于UGUI的DropDown点击区域外不关闭的问题
- (2)七层模型基础---设备
- JAVA获取执行sql
- dubbo -- 搭建dubbo-admin管理界面