实例介绍
【实例截图】
【核心代码】
package com.android.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginAct extends Activity {
private EditText name;
private EditText pwd;
private Button login;
private Button register;
private String questStr = "";
private String postStr = "";
private String serverIp="192.168.50.93";
private String serverPort="8080";
private Intent intent;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
name = (EditText) findViewById(R.id.UserName);
pwd = (EditText) findViewById(R.id.pwd);
login = (Button) findViewById(R.id.login);
register = (Button) findViewById(R.id.register);
intent = new Intent(LoginAct.this, RegisterAct.class);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (LoginAct.this.loginIsSuccsee()) {
// 用户输入正确
login();
}
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// name.setText("");
// pwd.setText("");
startActivity(intent);
}
});
}
/*
*
* 取得用户信息处理登录
*/
public void doLogin() {
EditText name = (EditText) findViewById(R.id.UserName);
EditText pwd = (EditText) findViewById(R.id.pwd);
String username = name.getText().toString();
String password = pwd.getText().toString();
// 传递到下个Activity
Bundle bundle = new Bundle();
bundle.putString("username", username);
bundle.putString("password", password);
bundle.putString("serverIp", "192.168.50.93");
bundle.putString("serverPort", "8080");
Intent intent = new Intent(LoginAct.this, LoginActivity.class);
// this.setResult(0, this.getIntent().putExtras(bundle));
intent.putExtras(bundle);
startActivity(intent);
this.finish();
}
/*
* 判断用户输入是否规范 录入信息验证 验证是否通过
*/
private boolean loginIsSuccsee() {
EditText name = (EditText) findViewById(R.id.UserName);
EditText pwd = (EditText) findViewById(R.id.pwd);
// 获取用户输入的信息
String userName = name.getText().toString();
String password = pwd.getText().toString();
if ("".equals(userName)) {
// 用户输入用户名为空
Toast.makeText(LoginAct.this, "用户名不能为空!", Toast.LENGTH_SHORT)
.show();
return false;
} else if ("".equals(password)) {
// 密码不能为空
Toast.makeText(LoginAct.this, "密码不能为空!", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
private void login() {
// 获取用户输入的用户名,密码
EditText name = (EditText) findViewById(R.id.UserName);
EditText pwd = (EditText) findViewById(R.id.pwd);
String username = name.getText().toString();
String password = pwd.getText().toString();
String httpstr = "http://";
postStr = "http://" serverIp ":" serverPort
"/WebLoginDemo/LoginServlet?username=" username "&password" password;
// questStr = "{LOGIN:{username:'}" username "',password:'"
// password "'}}";
questStr = "{LOGIN:{username:'" username "',password:'" password
"'}}";
System.out.println("====postStr====" postStr);
System.out.println("====questStr====" questStr);
try {
// 设置连接超时
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(postStr);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("username", username));
nvps.add(new BasicNameValuePair("password", password));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
String isUser = ConvertStreamToString(is);
System.out.println("asdfsdf" isUser);
String tb=isUser.substring(isUser.length()-4);
System.out.println(tb "===========");
if ("true".equals(tb)) {
// 登录成功
// 获取所有用户的数量
String numStr = isUser.substring(0, isUser.indexOf("t"));
int num = Integer.parseInt(numStr);
System.out.println("numStr" numStr);
Intent intent =new Intent(LoginAct.this,LoginActivity.class);
Bundle bundle=new Bundle();
Toast.makeText(this, "登录成功", Toast.LENGTH_LONG).show();
//用户数量
bundle.putInt("userNum", num);
intent.putExtras(bundle);
startActivity(intent);
this.finish();
} else if ("false".equals(isUser)) {
System.out.println("登录失败?");
AlertDialog.Builder builder = new Builder(LoginAct.this);
builder.setMessage("用户名或密码输入错误!请重新登录");
builder.setTitle("提示");
builder.setPositiveButton("确认", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Intent i=new
// Intent(LoginActivity.this,LoginAct.class);
// LoginActivity.this.finish();
// startActivity(i);
// EditText name = (EditText) findViewById(R.id.UserName);
// EditText pwd = (EditText) findViewById(R.id.pwd);
// name.setText("");
// pwd.setText("");
dialog.dismiss();
}
}).show();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 读取字符流
public String ConvertStreamToString(InputStream is) {
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String returnStr = "";
try {
while ((returnStr = br.readLine()) != null) {
sb.append(returnStr);
}
} catch (IOException e) {
e.printStackTrace();
}
final String result = sb.toString();
// System.out.println(result);
return result;
}
}
标签: 登录
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论