Android开发 httpClient post方法请求tomcat服务器实现注册登陆
来源:互联网 发布:知乎 东邪西毒讲的什么 编辑:程序博客网 时间:2024/06/06 02:40
package com.example.httplogintest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LoginActivity extends Activity {
private Button register_bn;
private Button login_bn;
private EditText user_et;
private EditText pwd_et;
private TextView tv1;
private int username_ok=1;//注册成功
private int username_already_exit=2;//注册失败,用户名已存在
private int register_failed=0;//注册失败
private int login_success=3;//登录成功
private int login_failed=4;//登录失败
private int username_pwd_null=5;//用户名或密码为空
Myhandler myHandler=new Myhandler();
String basehttpurl="http://192.168.0.103:8080/logintest/";
String subhttpurl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
register_bn=(Button)findViewById(R.id.register_bn);
login_bn=(Button)findViewById(R.id.login_bn);
user_et=(EditText)findViewById(R.id.user_et);
pwd_et=(EditText)findViewById(R.id.pwd_et);
tv1=(TextView)findViewById(R.id.tv1);
Myhandler myHandler=new Myhandler();
register_bn.setOnClickListener(new RegisterListener());
login_bn.setOnClickListener(new LoginListener());
}
public class RegisterListener implements OnClickListener{
@Override
public void onClick(View v) {
subhttpurl="register.jsp";
Loginthread thread=new Loginthread();
thread.start();
}
}
public class LoginListener implements OnClickListener{
@Override
public void onClick(View v) {
subhttpurl="login.jsp";
Loginthread thread=new Loginthread();
thread.start();
}
}
public class Myhandler extends Handler{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if(msg.what==username_ok)
Toast.makeText(LoginActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
if(msg.what==username_already_exit)
Toast.makeText(LoginActivity.this, "用户名已被使用", Toast.LENGTH_SHORT).show();
if(msg.what==login_success)
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
if(msg.what==login_failed)
Toast.makeText(LoginActivity.this, "请重新输入账号密码", Toast.LENGTH_SHORT).show();
if(msg.what==register_failed)
Toast.makeText(LoginActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
if( msg.what==username_pwd_null)
Toast.makeText(LoginActivity.this, "请输入账号密码", Toast.LENGTH_SHORT).show();
}
}
public class Loginthread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Message msg=new Message();
String httpUrl=basehttpurl+subhttpurl;
// 创建HttpPost连接对象
HttpPost httpRequest=new HttpPost(httpUrl);
// 使用NameValuePair来保存要传递的Post参数(键值对)
List parameter=new ArrayList();
String username=user_et.getText().toString().trim();
String pwd=pwd_et.getText().toString().trim();
// 添加要传递的参数
if(!username.equals("")&&!pwd.equals(""))
{
parameter.add(new BasicNameValuePair("username",username));
parameter.add(new BasicNameValuePair("pwd", pwd));
// 设置字符集
HttpEntity httpentity;
try {
httpentity = new UrlEncodedFormEntity(parameter, "gb2312");
// 请求httpRequset
httpRequest.setEntity(httpentity);
// 取得HttpClient
HttpClient httpClient = new DefaultHttpClient();
// 取得HttpResponse
HttpResponse httpResponse = httpClient.execute(httpRequest);
// HttpStatus.SC_OK表示连接成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String strResult = EntityUtils.toString(httpResponse.getEntity());
//创建JSONObject
JSONObject jsonobj=new JSONObject(strResult);
String Status=jsonobj.getString("Status");
if(Status.equals("username_ok"))
{ msg.what=username_ok;
System.out.println("1");}
else if(Status.equals("username_already_exit"))
msg.what=username_already_exit;
else if(Status.equals("login_success"))
msg.what=login_success;
else if(Status.equals("login_failed"))
msg.what=login_failed;
}
else
msg.what=register_failed;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
msg.what=username_pwd_null;
myHandler.sendMessage(msg);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
}
- Android开发 httpClient post方法请求tomcat服务器实现注册登陆
- Android开发 httpClient post方法请求tomcat服务器实现注册登陆
- Android开发 httpclient Post方法实现从tomcat服务器(JSP)获取listview信息
- android开发 使用HttpClient访问网络,post、get方式。注册、登陆、上传文件。
- HttpClient之Post通信与服务器连接实现登陆功能
- Android实现Get-Post登陆请求
- HttpClient请求服务器 get post
- Android使用Httpclient网络请求,get,post方法
- Android 实现HttpClient发起GET或者POST请求
- android使用Apache HttpClient实现get/post的请求
- Android用Apache HttpClient 实现POST和Get请求
- Android用Apache HttpClient 实现POST和Get请求
- Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- android利用httpclient实现post、get请求restful接口进行json和form表单数据提交等公共方法类
- HttpClient之实现POST方法
- HttpClient Post请求两种方法。
- SRM 617
- 响应式设计:如何使区块保持比例自动缩放
- 每天学习的点滴--c和指针
- 异或的奇妙
- “对不起,您安装的不是正版应用,安装程序无法继续执行 discuz”解决方法
- Android开发 httpClient post方法请求tomcat服务器实现注册登陆
- 单链表操作
- 编译ant compile-native遇到的一些问题
- 南阳理工OJ_题目915 +-字符串
- Tomcat运行完一个servlet后停止的解决
- Android之UtilsRequesServicetHelp工具类
- hdu2438 Turn the corner 几何 三分法
- 用VA快速添加注释+快捷键
- 【Mark】不是技术牛人如何拿到国内IT巨头Offer