Android手机连接服务器端实现登陆
2014-05-24 20:11
369 查看
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;
}
}
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;
}
}
相关文章推荐
- 使用android-async-http-master框架实现手机登陆服务器端密码验证
- 实现android手机通过WIFI和PC连接。
- 我的Android进阶之旅------>Android实现用Android手机控制PC端的关机和重启的功能(一)PC服务器端
- Android 使用Socket实现服务器与手机客户端的长连接五:使用队列封装请求
- Mina长连接框架实现Android客户端与服务器端通信
- 如何实现CAN总线设备无线连接Android手机
- android客户端向服务器端验证登陆方法的实现1
- Android通过Http连接MySQL 实现登陆/注册(数据库+service+客户端)【大部分转自帮客之家】
- Android 使用Socket实现服务器与手机客户端的长连接六:二次封装
- Android 使用Socket实现服务器与手机客户端的长连接二:多Client对一Server聊天
- Android-用户登陆的例子(服务器端开发到客户端实现)
- Android客户端+mysql+springmvc服务器端实现登陆的小案例
- Android-用户登陆的例子(服务器端开发到客户端实现)
- Android 使用Socket实现服务器与手机客户端的长连接七:定义消息体,区分是响应还是回复信息
- Android手机客户端通过JSP实现与Tomcat服务器端通信(Msql数据库,Json作为载体)--服务端代码
- Android使用Gson解析JSON连接服务器实现客户端登陆功能
- android手机客户端连接服务器实现交互
- android实现socket连接(服务器端)
- Android 使用Socket实现服务器与手机客户端的长连接八
- Android 使用Socket实现服务器与手机客户端的长连接七:定义消息体,区分是响应还是回复信息