利用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()); } }
相关文章推荐
- web前端开发:Http协议
- 修改ubuntu的apt-get软件源(ubuntu的网络问题)
- centos系统,基于nginx服务器,用https访问php页面总弹出下载页面问题完美解决
- ubuntu 14.04 网络图标消失解决方法 状态栏图标不显示
- Python的Tornado框架的异步任务与AsyncHTTPClient
- HTTP接口测试还可以这么玩
- 1、TCP/IP协议簇
- Java 判断一段网络资源是否存在
- Wireshark基本介绍及应用tcp
- multipart方式上传图片(HttpURLConnection)
- android OkHttp的使用
- 深入理解HTTP Session
- Android使用BroadcastReceiver监听网络状态 推荐
- Android 网络图片查看器
- Unity-WWW发送Http请求
- HttpClient 4 和 HttpClient 3 设置超时
- 网络配置网卡子接口
- HTTP状态码451:基于法律上的原因,我不能向你展示网页内容
- TCP/Protobuf之Protocol Buffers配置指南
- HTTP、TCP/IP协议与socket之间的区别