基本信息
源码名称:android 登录注册源码(含jsp服务端源码)
源码大小:1.62M
文件格式:.zip
开发语言:Java
更新时间:2016-03-28
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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;
}
}