Android客户端连接服务器
//Android端代码
public class LoginActivity extends Activity
{
EditText et_username,et_password;
TextView tv_errMsg;
Button btn_login;
String strUrl = "http://192.168.1.102:8080/Login/login_login2.action";
public static int REQUEST_SUCCESS_CODE = 200;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.proj2_login);
et_username = (EditText)findViewById(R.id.et_username);
et_password = (EditText)findViewById(R.id.et_password);
tv_errMsg = (TextView)findViewById(R.id.tv_errMsg);
btn_login = (Button)findViewById(R.id.btn_login);
btn_login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String result;
/* 发送部分 */
try {
HttpClient httpClient = new DefaultHttpClient();
String address = strUrl;
HttpPost httpPost = new HttpPost(address);
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("username", et_username.getText().toString()));
params.add(new BasicNameValuePair("password", et_password.getText().toString()));
httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中
HttpResponse httpResponse = httpClient.execute(httpPost);
/* 接收 */
if (httpResponse.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(httpResponse.getEntity());
/* json文本解析 */
JSONTokener jsonParser = new JSONTokener(result);
JSONObject jsonResult = (JSONObject) jsonParser.nextValue();
String status = jsonResult.getString("loginStatus");
String outMessage = jsonResult.getString("outMessage");
if(status.equals("true")){
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("username", outMessage);
startActivityForResult(intent, REQUEST_SUCCESS_CODE);
} else {
tv_errMsg.setVisibility(View.VISIBLE);
tv_errMsg.setText(outMessage);
}
} else {
tv_errMsg.setVisibility(View.VISIBLE);
tv_errMsg.setText("连接异常,CODE:" + httpResponse.getStatusLine().getStatusCode());
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
服务器端代码:采用SSH框架
public class LoginAction extends ActionSupport {
HttpServletRequest request;
HttpServletResponse response;
LoginService loginService;
public LoginService getLoginService() {
return loginService;
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
public String execute() throws Exception
{
request = ServletActionContext.getRequest();
request.setCharacterEncoding("utf-8"); //避免乱码
String name = request.getParameter("username");
String password = request.getParameter("password");
int result = loginService.validate(name, password);//验证用户名密码是否正确
JsonObject jsonObj = new JsonObject(); //须引入gson jar包
String loginStatus = "";
String outMessage = "";
if(result>0){
loginStatus = "true";
outMessage = "登录失败!";
}
else{
loginStatus = "false";
outMessage = "用户名或密码错误!";
}
jsonObj.addProperty("loginStatus", loginStatus);
jsonObj.addProperty("outMessage", outMessage);
response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");//避免乱码
PrintWriter out = response.getWriter();
out.write(jsonObj.toString());
//out.println(jsonObj.toString()); //也ok
return null;
}
}
更多相关文章
- Android 记住密码和自动登录界面的实现(SharedPreferences 的用法
- 【Android】debug 状态下其签名文件 debug.keystore 相关(如何获
- Android 在应用中使用用户凭证(PIN码、密码)
- Android(Java)正则表达式判断手机号、邮箱、身份证号码、密码、Url
- Android 仿支付宝密码输入页面
- 说说在 Android 中如何实现记住密码功能
- Android 音乐乱码问题修改
- 彻底解决Android中文乱码