Volley帮小弟我们省去的那些线程操作代码

Volley帮我们省去的那些线程操作代码



        最近两天刚开始用Volley,发现加上框架之后,代码精简了不少,被精简的代码主要体现在了:1,不用去创建新的线程类:一般我们在开启新线程的时候,都使用的是创建新的线程类,重写run方法,将可能会阻塞UI线程的操作放在新线程里面,当新线程执行完成之后,发送一个Message到UI线程,UI线程接收到消息之后,会对消息进行处理,从而完成我们的更新UI的操作。2,handler类:通常我们会在我们的Activity里面创建一个handler类,来处理我们的消息的发送与接收。使用了Volley之后,这两个类都省掉了,对于初学者上手也比较容易。

 



       Volley帮小弟我们省去的那些线程操作代码


下面以登录为例,看下代码结构,供初学者跟Volley作对比:



public class Login extends Activity implements OnClickListener {

	/* 界面控件 */
	private EditText accountEditText;// 用戶名
	private EditText pwdEdittext;// 密碼
	private Button btnLogin;// 登录按钮
	private ProgressBar processLogin;// 进度

	/* 标记信息 */
	private static final int MSG_SUCCESS = 0;// 登录成功标示
	private static final int MSG_FAILURE = 1;// 登录失败标示
	private static final int MSG_PwdIsWrong = 2;// 无法查询到用户

	private long mExitTime;

	/* 登录所用handler */
	private Handler mLoginHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_SUCCESS:
				// 登录成功之后跳转到home
				processLogin.setVisibility(View.GONE);
				Toast.makeText(getApplicationContext(), "登陆成功!",
						Toast.LENGTH_SHORT).show();

				Intent intent = new Intent();
				intent.setClass(Login.this, home.class);
				Login.this.startActivity(intent);
				finish();// 本页面finish掉

				// TODO:返回退出系统

				break;
			case MSG_FAILURE:
				Toast.makeText(getApplicationContext(), "服务器错误,请稍后重试!",
						Toast.LENGTH_SHORT).show();
				processLogin.setVisibility(View.GONE);
				btnLogin.setClickable(true);// 可点击
				break;
			case MSG_PwdIsWrong:
				Toast.makeText(getApplicationContext(), "用户名或密码错误!请重新输入!",
						Toast.LENGTH_SHORT).show();
				processLogin.setVisibility(View.GONE);
				pwdEdittext.setText("");// 清空pwd
				btnLogin.setClickable(true);// 可点击
				break;
			}
		}
	};

	/* 初始化页面控件 */
	private void initView() {
		processLogin = (ProgressBar) findViewById(R.id.processLogin);
		processLogin.setVisibility(View.GONE);
		accountEditText = (EditText) findViewById(R.id.accountEditText);
		accountEditText.setFocusable(true);
		pwdEdittext = (EditText) findViewById(R.id.pwdEdittext);
		btnLogin = (Button) findViewById(R.id.btnLogin);
		btnLogin.setOnClickListener(this);



	/* 创建Activity */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		initView();
	}

	/* 单击事件 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnLogin:
			processLogin.setVisibility(View.VISIBLE);
			String strAccount = accountEditText.getText().toString();
			String strPwd = pwdEdittext.getText().toString();
			
			/* 账号密码判空 */
			if (StringUtils.isEmpty(strAccount) && StringUtils.isEmpty(strPwd)) {
				Toast.makeText(getApplicationContext(), "请输入账号和密码!",
						Toast.LENGTH_SHORT).show();
				processLogin.setVisibility(View.GONE);
				break;
			}
			if (StringUtils.isEmpty(strAccount)) {
				Toast.makeText(getApplicationContext(), "账号不能为空!请输入账号!",
						Toast.LENGTH_SHORT).show();
				processLogin.setVisibility(View.GONE);
				break;
			}
			if (StringUtils.isEmpty(strPwd)) {
				Toast.makeText(getApplicationContext(), "密码不能为空!请输入密码!",
						Toast.LENGTH_SHORT).show();
				processLogin.setVisibility(View.GONE);
				break;
			}
			btnLogin.setClickable(false);// 设置登录按钮不可点击

			/* 登录 */
			new Thread() {
				Message msg = new Message();// 返给handler的信息

				@Override
				public void run() {
					// 获取全局对象Application
					AppContext appContext = (AppContext) getApplication();
					try {
						Operator_InfoVo operator_Info = appContext
								.ManagerLogin(accountEditText.getText()
										.toString(), StrManager
										.Encrypt(pwdEdittext.getText()
												.toString()));
						if (operator_Info == null) { // 读取数据出错
							msg.what = 2;
						} else {// 读取到接口数据
							/* 判断账户名密码 */
							// if
							// (operator_Info.getPasswd().toString().equals(pwdEdittext
							// .getText().toString())) {
							msg.what = 0;
							// 保存用户名密码
							SaveUserInfo(operator_Info.getOperator_Info()
									.getOperator_id().toString(), operator_Info
									.getOperator_Info().getName().toString(),
									operator_Info.getOperator_Info().getType()
											.toString());
							// } else {
							// msg.what = 2;
							// }

						}
					} catch (AppException e) {
						e.printStackTrace();
						msg.what = 1;
						msg.obj = e;
					}
					mLoginHandler.sendMessage(msg);
				}
			}.start();
			break;
		default:
			break;
		}
	}

	/* SharedPreference保存登录用户的信息 */
	public boolean SaveUserInfo(String UserID, String UserName,
			String UserType) {
		SharedPreferences sharedPreferences = getSharedPreferences(
				"smarlife-admin-app", Context.MODE_PRIVATE);// 使用默认的操作模式
		Editor editor = sharedPreferences.edit();// 返回一个Editor对象,用于操作sharedPreferences
		// 判断用户名密码是否存在
		if (sharedPreferences.contains("UserID")) {
			editor.remove("UserID");// 存在的情况下移除
		}
		editor.putString("UserType", UserType);
		editor.putString("UserName", UserName);
		editor.putString("UserID", UserID);
		return editor.commit();// 提交操作
	}

	/* 提示是否退出的对话框 */
	protected void dialog() {
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		finish();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			processLogin.setVisibility(View.GONE);
			dialog();
			return false;
		}
		return false;
	}

}





      在Volley里面:


           Volley帮小弟我们省去的那些线程操作代码


         

          对比以前的AJAX调用,只要重写回调后的成功或者失败的方法就可以了,而且不用时刻去考虑当前是不是在UI线程,能不能更新View的问题。




           






 

版权声明:本文为博主原创文章,未经博主允许不得转载。

1楼u010096526昨天 23:56
超哥好棒呢,volley确实精简代码啊