您的位置:首页 > 理论基础 > 计算机网络

利用AsyncHttpClient实现登陆

2016-06-27 16:27 351 查看
package com.xinke.app.beautifulcountry.base.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.xinke.app.beautifulcountry.R;
import com.xinke.app.beautifulcountry.base.config.AppConfig;
import com.xinke.app.beautifulcountry.core.BaseActivity;
import com.xinke.app.beautifulcountry.core.BeautifulCountryActivity;
import com.xinke.app.beautifulcountry.core.bean.UserInfo;
import com.xinke.app.beautifulcountry.core.configmanage.ConfigurationManager;
import com.xinke.app.beautifulcountry.utils.AsyncHttpClientHelper;
import com.xinke.app.beautifulcountry.utils.MD5Helper;
import com.xinke.app.beautifulcountry.utils.SharePrefUtil;

import org.json.JSONException;
import org.json.JSONObject;

/**
* @author 王德元
* @version V1.0
* @Description: 登录功能
* @date 2016/5/3 15:26
*/
public class LoginActivity extends BaseActivity implements View.OnClickListener {

public static String TAG = "deyuan";

private EditText usernameEdit;
private EditText passwordEdit;
private Button loginBtn;
private TextView registText;
private TextView forgetPasswordText;

private String username;
private String password;

private UserInfo mUser;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
initView();
}

@Override
protected void initView() {
usernameEdit = (EditText) findViewById(R.id.username_edit);
passwordEdit = (EditText) findViewById(R.id.password_edit);
loginBtn = (Button) findViewById(R.id.login_btn);
registText = (TextView) findViewById(R.id.regist_account);
forgetPasswordText = (TextView) findViewById(R.id.forget_password);

loginBtn.setOnClickListener(this);
registText.setOnClickListener(this);
forgetPasswordText.setOnClickListener(this);
}

@Override
public void onClick(View v) {

switch (v.getId()) {

case R.id.login_btn:

username = usernameEdit.getText().toString().trim();
password = passwordEdit.getText().toString().trim();

if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {

login(username, password);

} else {
Toast.makeText(this, getResources().getString(R.string.input_username_password), Toast.LENGTH_SHORT).show();
}

break;
case R.id.regist_account:

Intent intent = new Intent(this, RegisterActivity.class);
startActivity(intent);

break;
case R.id.forget_password:

break;
}

}

/**
* @param username 用户名    password 密码
* @return
* @throws
* @Description: 登录
* @author 王德元
* @date 2016/5/9 16:30
*/
void login(String username, String password) {

AsyncHttpClient client = AsyncHttpClientHelper.getInstance().getHttpClient();

String url = AppConfig.HTTP_URL + AppConfig.PATH_USER_LOGIN;
RequestParams requestParams = new RequestParams();
requestParams.put("memberPhone", username);
requestParams.put("memberPassword", MD5Helper.MD5(password));

mClient.post(url, requestParams, new JsonHttpResponseHandler() {

@Override
public void onSuccess(int statusCode, JSONObject response) {
Log.i(TAG, "statusCode----" + statusCode + "---JSONObject-->" + response.toString());

try {

int ret = parseJsonObject(response);

if (ret == 1) {
saveUserInfo(mUser);    //保存用户信息到数据库
Intent intent = new Intent(LoginActivity.this, BeautifulCountryActivity.class);
intent.putExtra("userInfo", mUser);
startActivity(intent);
finish();
}

} catch (JSONException e) {
e.printStackTrace();
}
}

@Override
public void onFailure(Throwable e, JSONObject errorResponse) {
if(errorResponse == null)
Toast.makeText(LoginActivity.this, R.string.network_connection_is_not_available, Toast.LENGTH_SHORT).show();
}

});
}

/**
* @param response 响应对象
* @return
* @throws
* @Description: 解析响应结果
* @author 王德元
* @date 2016/5/9 13:24
*/
private int parseJsonObject(final JSONObject response) throws JSONException {

int recode = -1;
if (response == null) {

return recode;
}
recode = response.getInt("recode");
if (recode == 0) {
Toast.makeText(this, getResources().getString(R.string.account_or_password_error), Toast.LENGTH_SHORT).show();
} else if (recode == 1) {
Toast.makeText(this, getResources().getString(R.string.login_success), Toast.LENGTH_SHORT).show();

JSONObject result = response.getJSONObject("result");
JSONObject bean = result.getJSONObject("bean");

mUser = JSON.parseObject(bean.toString(), UserInfo.class);
// 2016.5.30 zhenghw add start
SharePrefUtil.saveString(this, AppConfig.SHARE_PREF_USERID, mUser.getId());
SharePrefUtil.saveString(this, AppConfig.USER_ROLE, mUser.getMemberRole());
// 2016.5.30 zhenghw add end
}

return recode;
}

private void saveUserInfo(UserInfo user) {
ConfigurationManager.setUserId(this, user.getId());
ConfigurationManager.setUserRole(this, user.getMemberRole());
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: