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();