public class LoginActivity extends Activity {
private EditText et_name;
private EditText et_pass;
private TextView tv_result;
private final int CHANGETEXTVIEW = 1;
// 消息处理者
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int what = msg.what;// 标识
switch (what) {
case CHANGETEXTVIEW:
String result = (String) msg.obj;
//设置结果
tv_result.setText(result);
//吐司结果
Toast.makeText(LoginActivity.this,result,1).show();
break;
default:
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
et_name = (EditText) findViewById(R.id.username);
et_pass = (EditText) findViewById(R.id.userpass);
tv_result = (TextView) findViewById(R.id.textView);
}
public void login(View v) {
int id = v.getId();
switch (id) {
case R.id.login:
final String userName = et_name.getText().toString();
final String userPass = et_pass.getText().toString();
if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(userPass)) {
Toast.makeText(this, "用户名或者密码不能为空", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "发送请求到服务器", Toast.LENGTH_LONG).show();
new Thread() {
public void run() {
try {
//请求的地址
String spec = "http://172.16.237.119:8989/Login/login.do";
//根据地址创建URL对象
URL url = new URL(spec);
//根据URL对象打开链接
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//设置请求方式
urlConnection.setRequestMethod("POST");
urlConnection.setReadTimeout(5000);// 设置超时时间
urlConnection.setConnectTimeout(5000);// 设置连接时间
String data = URLEncoder.encode(userName,"UTF-8") + "&userpass=" + URLEncoder.encode(userPass,"UTF-8");
//设置请求头
urlConnection.setRequestProperty("Connection", "Keep-Alive");
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Content-Length", String.valueOf(data.getBytes().length));
urlConnection.
setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0E; InfoPath.3; .NET4.0C)");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
OutputStream os = urlConnection.getOutputStream();
/*DataOutputStream dataOutputStream = new DataOutputStream(os);
dataOutputStream.write(buffer);*/
os.write(data.getBytes());
System.out.println("******************"+data+"***********");
os.flush();
if(urlConnection.getResponseCode()==200){
// 输入流对象
InputStream is = urlConnection.getInputStream();
// 通过工具类处理
String result = StreamTools.streamToStr(is);
Message msg = new Message();
msg.what = CHANGETEXTVIEW;
msg.obj = result;// 改变的内容,obj带过去
handler.sendMessage(msg);//发送消息
}else{
System.out.println("连接失败...........");
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
break;
default:
break;
}
}
public void get(String userName,String userPass){
try {
// 请求地址
String spec = "http://172.16.237.119:8989/Login/login.do?username="
+ URLEncoder.encode(userName,"UTF-8") + "&userpass=" + URLEncoder.encode(userPass,"UTF-8");
// 根据地址创建URL对象(网络访问的url)
URL url = new URL(spec);
// url.openConnection()打开网络连接
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");// 设置请求方式
urlConnection.setReadTimeout(5000);// 设置超时时间
urlConnection.setConnectTimeout(5000);// 设置连接时间
urlConnection
.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0E; InfoPath.3; .NET4.0C");
// 获取相应的code 404 200 505 302
if (urlConnection.getResponseCode() == 200) {
// 输入流对象
InputStream is = urlConnection.getInputStream();
// 通过工具类处理
String result = StreamTools.streamToStr(is);
Message msg = new Message();
msg.what = CHANGETEXTVIEW;
msg.obj = result;// 改变的内容,obj带过去
handler.sendMessage(msg);//发送消息
}else{
System.out.println("链接失败");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("连接失败...........");
}
}
}
其中运用到工具类:Streamstools
public static String streamToStr(InputStream is){
try{
// 字节输出流
ByteArrayOutputStream os = new ByteArrayOutputStream();
// 定义读取长度
int len = 0;
// 定义缓冲区
byte buffer[] = new byte[1024];
// 从输入流中读取,并写入到os中
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
is.close();
os.close();
// 写到字节流
return new String(os.toByteArray(),"gbk");
//return new String(os.toByteArray());
}catch(Exception e){
e.printStackTrace();
return null;
}
}