都是本人原创 只是我提前在EOE论坛发布了。
{ “result”: 1, “uid”:22771,”msg”:””}
result=1 注册成功, uid为玩家uid, msg为空
result>1时注册失败, 此时返回的 uid=0
result=2: msg:用户名格式不对
result=3: msg:此用户名已经被注册
result=4: msg:密码格式不对(长度不是6-16位或者包含了其他字符,比如中文标点之类)
result=5: msg:广告来源为空 from 的值不允许为空
result=6: msg:系统维护,此时不允许注册
result>6 时为其他错误, msg会返回错误的具体原因
boolean flag ;//条件成立跳转到登陆界面
/** 请求服务器 */
if (userName != null || password != null || from != null) {
flag = UserDataServiceHelper.Register(context, "reg", userName, password, from);
if(flag){
Intent intent = new Intent();
intent.putExtra("name", userName);
intent.putExtra("pw", password);
intent.putExtra("fm",from);
intent.setClass(Register.this, Login.class);
startActivity(intent);
}else {
Log.i("TAG", "不成立");
}
Log.i("TAG", "请求服务器" + userName + password + from);
}
/** 初始化注册VIEW组件 */
private void findViewById() {
view_userName = (EditText) findViewById(R.id.loginUserNameEdit);
view_password = (EditText) findViewById(R.id.loginPasswordEdit);
view_rememberMe = (CheckBox) findViewById(R.id.loginRememberMeCheckBox);
view_loginSubmit = (Button) findViewById(R.id.loginSubmit);
view_loginRegister = (Button) findViewById(R.id.loginRegister);
view_fast = (TextView) findViewById(R.id.fast);
/** 注册成功后传过来用户名和密码,显示在登录界面 */
if (!flag) {
Intent intent = getIntent();
userName = intent.getStringExtra("name");
password = intent.getStringExtra("pw");
from = intent.getStringExtra("fm");
view_rememberMe.setChecked(false);//小BUG
view_userName.setText(userName);
view_password.setText(password);
}
}
view_userName.setText(userName);
view_password.setText(password);
/** 登录Button Listener */
private OnClickListener submitListener = new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("TAG", "submitListener");
proDialog = ProgressDialog.show(Login.this, "连接中..",
"连接中..请稍后....", true, true);
// 开启一个线程进行登录验证,主要是用户失败成功可以直接通过startAcitivity(Intent)转向
Thread loginThread = new Thread(new LoginFailureHandler());
loginThread.start();// 开启
}
};
// 另起一个线程登录
class LoginFailureHandler implements Runnable {
@Override
public void run() {
userName = view_userName.getText().toString();
Log.i("TAG", "userName LoginFailureHandler" + userName);
password = view_password.getText().toString();
Log.i("TAG", "password LoginFailureHandler" + password);
/** 请求服务器 */
if (userName != null || password != null) {
boolean loginState = UserDataServiceHelper.logins(context, "login", userName,
password, from);
Log.i("TAG", "登录返回条件" + loginState);
// 登录成功
if (loginState) {
String LoginUerId = UserDataService.LoginUid;
// 需要传输数据到登陆后的界面,
Intent intent = new Intent();
intent.setClass(Login.this, IndexPage.class);
Bundle bundle = new Bundle();
bundle.putString("LOGIN_USERNAME", userName);
bundle.putString("LOGIN_PASSWORD", password);
bundle.putString("LOGIN_ID", LoginUerId);
intent.putExtras(bundle);
// 转向登陆后的页面
startActivity(intent);
// /** 得到请求服务器返回码 */
String loginStateInt = UserDataService.results;
int Less = Integer.valueOf(loginStateInt); // 转换成整形
Log.i("TAG", "登录后的返回码:" + Less);
if (Less == 1) {
StatusCode = true;
}
// 登录成功记住帐号密码
if (StatusCode) {
if (isRememberMe()) {
saveSharePreferences(true, true);
} else {
saveSharePreferences(true, false);
}
} else {
// 如果不是网络错误
if (!isNetError) {
clearSharePassword();
clearShareName();
}
}
if (!view_rememberMe.isChecked()) {
clearSharePassword();
clearShareName();
}
proDialog.dismiss();
} else {
// 通过调用handler来通知UI主线程更新UI,
Log.i("TAG", "连接失败");
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putBoolean("isNetError", isNetError);
message.setData(bundle);
loginHandler.sendMessage(message);
}
}
}
}
附件里面我把接口那部分已经删掉了 你们可以看上面我写的请求服务器的类 根据自己的业务需求来添加和删除 请求服务器类写的一个不好的地方就是 每个方法都加了static 呵呵 这个原因主要是 我写了一个方法 然后剩下的接口我就复制上面的 然后改改。
public class UserDataServiceHelper {
/** 时间戳 */
static long serial = new Date().getTime();
/** KEY */
static String key = "后台人员会给你个密钥";
/** 网站地址 */
private static final String HOST_IP = "你的请求地址";
/** 请求服务器 S1,S2,S3等 */
static String server = "s1";
/** 返回码 1=成功 */
public static String results = null;
/** 登录UID */
public static String LoginUid = null;
/** 注册UID */
public static String RegisterUid = null;
/** 快速注册根据返回得到用户名 */
public static String FastUserName = null;
/** 快速注册根据返回得到密码 */
public static String FastPassWord = null;
/** 快速注册根据返回UID */
public static String Uid = null;
/** 充值请求地址*/
private static final String HOST_IP_CREDIT = "你的充值请求地址";
// 注册
public static boolean Register(Context context, String action, String username,
String password, String from) {
Log.i("TAG", "得到的" + context + action + username + password + from);
try {
HttpClient httpClient = new DefaultHttpClient();
// 请求超时
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
// 读取超时
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
StringBuilder uri = new StringBuilder();
uri.append(HOST_IP + "XXXXXX" + "?" + "action=" + action);
uri.append("&username=");
uri.append(username);
uri.append("&password=");
uri.append(password);
uri.append("&from=");
uri.append(from);
uri.append("&server=");
uri.append(server);
uri.append("&apitime=");
uri.append(serial);
uri.append("&key=");
uri.append(getMD5Str(serial + key));
Log.i("TAG", "请求地址:" + uri.toString());
HttpPost httpPost = new HttpPost(uri.toString());
HttpResponse httpResponse = httpClient.execute(httpPost);
String jsonforString = null;
String result = null;
String result2 = null;
String result3 = null;
String result4 = null;
String result5 = null;
String result6 = null;
String result7 = null;
// 返回json报文
if (httpResponse.getStatusLine().getStatusCode() == 200) {
jsonforString = EntityUtils.toString(httpResponse.getEntity());
JSONObject jsonObject = new JSONObject(jsonforString);
Log.i("TAG", "JSONObject对象=" + jsonforString);
result = jsonObject.getString("result");
result2 = jsonObject.getString("msg");
Log.i("TAG", "result2:返回的错误信息"+result2);
Log.i("TAG", "成功还是失败返回" + result);
if (httpClient != null) {
httpClient.getConnectionManager().shutdown();
}
if (result.equalsIgnoreCase("1")) {
return true;
} else {
return false;
}
}
} catch (ConnectException e) {
Toast.makeText(context, "网络连接异常", Toast.LENGTH_LONG).show();
} catch (SocketTimeoutException e) {
Toast.makeText(context, "连接超时", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// 登录
public static boolean logins(Context context, String action, String username, String password,
String from) {
Log.i("TAG", "得到的" + context + action + username + password);
try {
HttpClient httpClient = new DefaultHttpClient();
// 请求超时
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
// 读取超时
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
StringBuilder uri = new StringBuilder();
uri.append(HOST_IP + "XXXXXXXX" + "?" + "action=" + action);
uri.append("&username=");
uri.append(username);
uri.append("&password=");
uri.append(password);
uri.append("&from=");
uri.append(from);
uri.append("&server=");
uri.append(server);
uri.append("&apitime=");
uri.append(serial);
uri.append("&key=");
uri.append(getMD5Str(serial + key));
Log.i("TAG", "请求地址:" + uri.toString());
HttpPost httpPost = new HttpPost(uri.toString());
HttpResponse httpResponse = httpClient.execute(httpPost);
String jsonforString = null;
String result = null;
String login = null;
// 返回json报文
if (httpResponse.getStatusLine().getStatusCode() == 200) {
jsonforString = EntityUtils.toString(httpResponse.getEntity());
JSONObject jsonObject = new JSONObject(jsonforString);
Log.i("TAG", "JSONObject对象=" + jsonforString);
result = jsonObject.getString("result");
login = jsonObject.getString("uid");
Log.i("TAG", "成功还是失败返回" + result);
if (httpClient != null) {
httpClient.getConnectionManager().shutdown();
}
if (result.equalsIgnoreCase("1")) {
Log.i("TAG", "服务器返回码" + result);
results = result;
LoginUid = login;
return true;
} else {
return false;
}
}
} catch (ConnectException e) {
Toast.makeText(context, "网络连接异常", Toast.LENGTH_LONG).show();
} catch (SocketTimeoutException e) {
Toast.makeText(context, "连接超时", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// 快速注册
public static boolean fast(Context context, String action, String from) {
try {
HttpClient httpClient = new DefaultHttpClient();
// 请求超时
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
// 读取超时
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
StringBuilder uri = new StringBuilder();
uri.append(HOST_IP + "XXXXXXXX" + "?" + "action=" + action);
uri.append("&from=");
uri.append(from);
uri.append("&server=");
uri.append(server);
uri.append("&apitime=");
uri.append(serial);
uri.append("&key=");
uri.append(getMD5Str(serial + key));
Log.i("TAG", "快速注册请求地址:" + uri.toString());
HttpPost httpPost = new HttpPost(uri.toString());
HttpResponse httpResponse = httpClient.execute(httpPost);
String jsonforString = null;
String result = null;
String UserNames = null;
String PassWords = null;
String UserId = null;
// 返回json报文
if (httpResponse.getStatusLine().getStatusCode() == 200) {
jsonforString = EntityUtils.toString(httpResponse.getEntity());
JSONObject jsonObject = new JSONObject(jsonforString);
Log.i("TAG", "JSONObject对象=" + jsonforString);
result = jsonObject.getString("result");
Log.i("TAG", "快速注册成功还是失败返回码" + result);
UserNames = jsonObject.getString("username");
PassWords = jsonObject.getString("password");
Log.i("TAG", "快速注册成功返回的密码" + PassWords);
Log.i("TAG", "快速注册成功返回用户名" + UserNames);
UserId = jsonObject.getString("uid");
if (httpClient != null) {
httpClient.getConnectionManager().shutdown();
}
if (result.equalsIgnoreCase("1")) {
Log.i("TAG", "快速注册服务器返回码" + result);
results = result;
FastUserName = UserNames;
FastPassWord = PassWords;
Uid = UserId;
return true;
} else {
return false;
}
}
} catch (ConnectException e) {
Toast.makeText(context, "网络连接异常", Toast.LENGTH_LONG).show();
} catch (SocketTimeoutException e) {
Toast.makeText(context, "连接超时", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// 修改帐号
public static boolean updateData(Context context, String action, String uid, String username,
String password, String newname, String newpass) {
try {
HttpClient httpClient = new DefaultHttpClient();
// 请求超时
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
// 读取超时
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
StringBuilder uri = new StringBuilder();
uri.append(HOST_IP + "XXXXXXXXX" + "?" + "action=" + action);
uri.append("&uid=");
uri.append(uid);
uri.append("&username=");
uri.append(username);
uri.append("&password=");
uri.append(password);
uri.append("&newname=");
uri.append(newname);
uri.append("&newpass=");
uri.append(newpass);
uri.append("&server=");
uri.append(server);
uri.append("&apitime=");
uri.append(serial);
uri.append("&key=");
uri.append(getMD5Str(serial + key));
Log.i("TAG", "提交更新:" + uri.toString());
HttpPost httpPost = new HttpPost(uri.toString());
HttpResponse httpResponse = httpClient.execute(httpPost);
String jsonforString = null;
String result = null;
String UserNames = null;
String PassWords = null;
String UserId = null;
// 返回json报文
if (httpResponse.getStatusLine().getStatusCode() == 200) {
jsonforString = EntityUtils.toString(httpResponse.getEntity());
JSONObject jsonObject = new JSONObject(jsonforString);
// Log.i("TAG", "JSONObject对象=" + jsonforString);
result = jsonObject.getString("result");
Log.i("TAG", "提交更新返回码" + result);
// UserNames = jsonObject.getString("username");
// PassWords = jsonObject.getString("password");
// Log.i("TAG", "快速注册成功返回的密码" + PassWords);
// Log.i("TAG", "快速注册成功返回用户名" + UserNames);
UserId = jsonObject.getString("uid");
if (httpClient != null) {
httpClient.getConnectionManager().shutdown();
}
if (result.equalsIgnoreCase("1")) {
Log.i("TAG", "提交更新" + result);
// results = result;
// FastUserName = UserNames;
// FastPassWord = PassWords;
// Uid = UserId;
return true;
} else {
return false;
}
}
} catch (ConnectException e) {
Toast.makeText(context, "网络连接异常", Toast.LENGTH_LONG).show();
} catch (SocketTimeoutException e) {
Toast.makeText(context, "连接超时", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// MD5
public static String getMD5Str(String str) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(str.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] byteArray = messageDigest.digest();
StringBuffer md5StrBuff = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
if (Integer.toHexString(0xFF & byteArray).length() == 1) {
md5StrBuff.append("0").append(
Integer.toHexString(0xFF & byteArray));
} else {
md5StrBuff.append(Integer.toHexString(0xFF & byteArray));
}
}
return md5StrBuff.toString();
}
}
项目下载地址:
http://download.csdn.net/detail/zxciop110/4737112