IOS下载
android下载(安智)
参考链接
http://blog.csdn.net/lamp_zy/article/details/52300629
//第一版系统登录接口:将手机号和密码通过POST方式发送给服务器,参数放入body中
private void login(final String phone, final String pwd){
if(GlobalConstants.DEBUG) { //开启调试
PrefUtils.setString(LoginActivity.this, "USER_NAME", "13135678021");
PrefUtils.setString(LoginActivity.this, "PASSWORD", "1234");
logLogin.log2File(tag, "开启调试");
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(intent);
finish();
}else {
HttpUtils utils = new HttpUtils(GlobalConstants.HTTP_TIMEOUT);
RequestParams params = new RequestParams();
params.addBodyParameter("phone", phone);
params.addBodyParameter("password", pwd);
utils.send(HttpRequest.HttpMethod.POST, GlobalConstants.SERVER + "login", params, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
Gson gson = new Gson();
mResponse = gson.fromJson(responseInfo.result, Response.class);
//System.out.println("服务器返回值"+ result);
if (TextUtils.equals(mResponse.result, GlobalConstants.SUCCESS)) {
//登录成功就在sharedPreference记住用户名、密码、
PrefUtils.setString(LoginActivity.this, "USER_NAME", phone);
PrefUtils.setString(LoginActivity.this, "PASSWORD", pwd);
logLogin.log2File(tag, "login ok");
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(intent);
finish();
} else {
logLogin.log2File(tag, "login failed");
Toast.makeText(LoginActivity.this, "用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(HttpException e, String s) {
logLogin.log2File(tag, "login with[]" + e);
Toast.makeText(LoginActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
}
});
}
}
//第二版系统登录接口WEB Service方式:参数放入JSON中
private void loginJson(final String phone, final String pwd) throws UnsupportedEncodingException {
HttpUtils utils = new HttpUtils(GlobalConstants.HTTP_TIMEOUT);
RequestParams params = new RequestParams();
params.addHeader("Content-Type","application/json");
JSONObject json = new JSONObject();
try{
json.put("phone", phone);
json.put("password", pwd);
}catch (JSONException e){
e.printStackTrace();
}
params.setBodyEntity(new StringEntity(json.toString(),"utf-8"));
utils.send(HttpRequest.HttpMethod.POST, GlobalConstants.SERVER + "login", params, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
Gson gson = new Gson();
mResponse = gson.fromJson(responseInfo.result, Response.class);
//System.out.println("服务器返回值"+ result);
if (TextUtils.equals(mResponse.result, GlobalConstants.SUCCESS)) {
//登录成功就在sharedPreference记住用户名、密码、
PrefUtils.setString(LoginActivity.this, "USER_NAME", phone);
PrefUtils.setString(LoginActivity.this, "PASSWORD", pwd);
logLogin.log2File(tag, "login ok");
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(intent);
finish();
} else {
logLogin.log2File(tag, "login failed");
Toast.makeText(LoginActivity.this, "用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(HttpException e, String s) {
logLogin.log2File(tag, "login with[]" + e);
Toast.makeText(LoginActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
}
});
}
if (1 == GlobalConstants.VERSION_NUM) {
login(userNameValue,passwordValue);
}else {
try {
loginJson(userNameValue, passwordValue);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}