用户登录

一共两个类:

package com.tcrj.spurmountaion.net;

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

import android.content.Context;

import com.tcrj.core.loader.Loader.LoaderListener;
import com.tcrj.spurmountaion.application.BaseApplication;
import com.tcrj.spurmountaion.entity.UserInfoEntity;
import com.tcrj.spurmountaion.utils.JsonParse;
import com.tcrj.spurmountaion.utils.NetUtil;

/**
 * 主账号登录
 * 
 * @author leict
 * 
 */
public class GovernmentCallBack {
	/**
	 * 登录
	 */
	public static final int FAILURE = 0;
	public static final int SUCCESS = 1;
	public static final int SERVICE = 2;

	public void loadLogin(final Context context, String account,
			String password, final OnGovernMentCallBack listener) {
		JsonLoader loader = new JsonLoader(context);
		loader.setUrl(NetUtil.getLoginInfo());
		loader.setPostData(setPostParmes(account, password));
		loader.setLoaderListener(new LoaderListener() {

			@Override
			public void onProgress(Object tag, long curByteNum,
					long totalByteNum) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onError(Object tag, int responseCode,
					String errorMessage) {
				listener.setGovernmentLister(SERVICE, null);
			}

			@Override
			public void onCompelete(Object tag, Object result) {
				JSONArray array = (JSONArray) result;
				UserInfoEntity entity = JsonParse.getUserLogin(array);
				if (entity == null) {
					listener.setGovernmentLister(FAILURE, entity);
				} else {
					listener.setGovernmentLister(SUCCESS, entity);
				}
			}
		});
		BaseApplication.getDataStratey().startLoader(loader);
	}

	public interface OnGovernMentCallBack {
		public void setGovernmentLister(int status, UserInfoEntity entity);
	}

	private String setPostParmes(String account, String password) {
		JSONObject json = new JSONObject();
		try {
			json.put("AccountName", account);
			json.put("LoaPwd", password);
		} catch (JSONException e) {
		}
		return json.toString();
	}
}

****************************************************************************************************************************

package com.tcrj.spurmountaion.activitys;

import com.tcrj.spurmountaion.application.BaseApplication;
import com.tcrj.spurmountaion.entity.UserInfoEntity;
import com.tcrj.spurmountaion.net.GovernmentCallBack;
import com.tcrj.spurmountaion.net.GovernmentCallBack.OnGovernMentCallBack;
import com.tcrj.spurmountaion.utils.CodeUtils;
import com.tcrj.spurmountaion.utils.ContextUtils;
import com.tcrj.spurmountaion.utils.IntentClassChange;
import com.tcrj.spurmountaion.utils.Utils;
import com.tcrj.views.MyProgressBar;

import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 政务管理服务平台
 * 
 * @author leict
 * 
 */
public class LoginGovernmentActivity extends BaseActivity implements
		OnClickListener {
	private Button login_government = null;
	private TextView txtTitle = null;
	private ImageButton imgBack = null;
	private String title;
	private EditText account = null;
	private EditText password = null;
	private MyProgressBar progressBar = null;
	private EditText input_verification = null;
	private ImageView verificationimage = null;
	private TextView changecode = null;

	@Override
	protected void initCreate(boolean hasScrollView) {
		super.initCreate(false);
		View titleView = ContextUtils.getLayoutInflater(this).inflate(
				R.layout.item_title, null);
		win_title.addView(titleView);
		View contentView = ContextUtils.getLayoutInflater(this).inflate(
				R.layout.logingovernment, null);
		win_content.addView(contentView);
		Intent intent = getIntent();
		title = intent.getStringExtra("Title");
		findViewById();
	}

	private void findViewById() {
		input_verification = (EditText) findViewById(R.id.input_verification);
		verificationimage = (ImageView) findViewById(R.id.verificationimage);
		changecode = (TextView) findViewById(R.id.changecode);
		login_government = (Button) findViewById(R.id.btn_govera_login);
		txtTitle = (TextView) findViewById(R.id.txtTitle);
		imgBack = (ImageButton) findViewById(R.id.imgbtn_back);
		account = (EditText) findViewById(R.id.edt_goveraccount);
		password = (EditText) findViewById(R.id.edt_goverpassword);
		input_verification = (EditText) findViewById(R.id.input_verification);
		progressBar = (MyProgressBar) findViewById(R.id.loading_government);
		login_government.setOnClickListener(this);
		txtTitle.setText(title);
		changecode.setText(Html.fromHtml("<u>" + "换一张" + "</u>"));
		verificationimage
				.setImageBitmap(CodeUtils.getInstance().createBitmap());
		imgBack.setVisibility(View.VISIBLE);
		imgBack.setOnClickListener(this);
		changecode.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_govera_login:
			if (!ContextUtils.isNetWorking(this)) {
				displayToast("请检查网络是否可用!");
				return;
			} else {
				login();
			}
			break;
		case R.id.imgbtn_back:
			finish();
			break;
		case R.id.changecode:
			verificationimage.setImageBitmap(CodeUtils.getInstance()
					.createBitmap());
			break;
		}
	}

	public void login() {
		progressBar.setVisibility(View.VISIBLE);
		progressBar.setProgressInfo("正在登录...");
		final String verificationstr = input_verification.getText().toString()
				.trim();
		final String userAccount = account.getText().toString().trim();
		final String userPassword = password.getText().toString().trim();
		if (Utils.isStringEmpty(userAccount)) {
			displayToast("账号不能为空");
			progressBar.setVisibility(View.GONE);
			return;
		}
		if (Utils.isStringEmpty(userPassword)) {
			displayToast("密码不能为空");
			progressBar.setVisibility(View.GONE);
			return;
		}
		if (!verificationstr
				.equalsIgnoreCase(CodeUtils.getInstance().getCode())) {
			displayToast("验证码不正确");
			progressBar.setVisibility(View.GONE);
			return;
		}
		new GovernmentCallBack().loadLogin(LoginGovernmentActivity.this,
				userAccount, userPassword, new OnGovernMentCallBack() {

					@Override
					public void setGovernmentLister(int status,
							UserInfoEntity entity) {
						progressBar.setVisibility(View.GONE);
						if (status == GovernmentCallBack.SUCCESS) {
							displayToast("登录成功");
							UserInfoEntity user = entity;
							user.setUserName(userAccount);
							user.setUserPwd(userPassword);
							user.setRoleID(entity.getRoleID());
							BaseApplication.setUserInfoEntity(user,
									LoginGovernmentActivity.this);
							/*
							 * Intent intent = new Intent(
							 * LoginGovernmentActivity.this,
							 * MainTabActivity.class); intent.putExtra("Tab",
							 * 4); startActivity(intent);
							 */
							IntentClassChange
									.startMobileOffice(LoginGovernmentActivity.this);
							finish();
						} else if (status == GovernmentCallBack.SERVICE) {
							displayToast("请求超时,重新登录");
							return;
						} else {
							displayToast("用户名或者密码错误");
							return;
						}
					}
				});
	}
}

  

posted @ 2016-01-06 13:19  逍遥散人95  阅读(225)  评论(0编辑  收藏  举报