码迷,mamicode.com
首页 > 其他好文 > 详细

一套非常标准的数据请求和数据展示代码的模板

时间:2015-05-01 17:18:43      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

package com.anzi.jmsht.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.json.JSONObject;

import com.anzi.jmsht.AppException;
import com.anzi.jmsht.AppManager;
import com.anzi.jmsht.Constant;
import com.anzi.jmsht.net.xml.api.Net;
import com.anzi.jmsht.ui.R;
import com.anzi.jmsht.util.Constants;
import com.anzi.jmsht.util.DataCheckout;
import com.anzi.jmsht.util.PreferencesHelper;
import com.anzi.jmsht.view.ExtDialog;
import com.anzi.jmsht.widget.AppStartBgService;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 登录
 * 
 * @author hhh
 * 
 */
public class LoginActivity extends Activity implements OnClickListener {

	private Button regist, login;
	private EditText userN_et, pw_et;
	private TextView forgetPw;
	private ExecutorService fixedThreadPool;
	private ArrayList<Map<String, Object>> list;// 存放登录信息
	private ProgressDialog dialog;
	private PreferencesHelper ph;
	private ExtDialog extDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login_activity_layout);
		// 注册默认的未捕捉异常处理类
		Thread.setDefaultUncaughtExceptionHandler(AppException
				.getAppExceptionHandler());
		AppManager.getAppManager().addActivity(this);
		// 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待:
		fixedThreadPool = Executors.newFixedThreadPool(1);
		ph = new PreferencesHelper(this, Constants.ANZIDate);
		try {
			update();
			initView();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (fixedThreadPool != null) {
			fixedThreadPool.shutdown();
		}
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
	}
	
	private void update() {
	        String url = ph.getValue("URL");
		String note = ph.getValue("NOTE");
		String size = ph.getValue("SIZE");
		String type = ph.getValue("TYPE");
		String version = ph.getValue("version");
		// Toast.makeText(getApplicationContext(), url+note+size+"这里__"+label,
		// 0).show();
		if ("1".equals(type)) {
			extDialog = new ExtDialog(this, "文件大小:" + size, note, url, type,
					version);
			extDialog.setCancelable(false);
			extDialog.show();
			// Intent intent = new Intent(this,UpdateService.class);
			// intent.putExtra("Key_App_Name", "摇乾树");
			// intent.putExtra("Key_Down_Url",url);
			// this.startService(intent);
		} else if ("0".equals(type)) {
			extDialog = new ExtDialog(this, "文件大小:" + size, note, url, type,
					version);
			extDialog.show();
		}

		 }
	}

	/*
	 * 初始化控件
	 */
	private void initView() {

		SharedPreferences preference = getSharedPreferences("person",
				Context.MODE_PRIVATE);
		regist = (Button) findViewById(R.id.regist);
		regist.setOnClickListener(this);
		login = (Button) findViewById(R.id.login);
		login.setOnClickListener(this);
		userN_et = (EditText) findViewById(R.id.userN_et);
		// userN_et.setText(preference.getString("phoneNumber",""));
		pw_et = (EditText) findViewById(R.id.pw_et);
		// pw_et.setText(preference.getString("password",""));
		forgetPw = (TextView) findViewById(R.id.forgetPw);
		forgetPw.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.regist:// 注册
			((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
					.hideSoftInputFromWindow(LoginActivity.this
							.getCurrentFocus().getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);
			startActivity(new Intent(getApplicationContext(),
					RegistActivity.class));
			break;
		case R.id.login:// 登录
			((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
					.hideSoftInputFromWindow(LoginActivity.this
							.getCurrentFocus().getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);
			list = new ArrayList<Map<String, Object>>();
			// startActivity(new Intent(getApplicationContext(),
			// HomeTabActivity.class));
			// AppManager.getAppManager().finishActivity();
			String phoneNumber = userN_et.getText().toString();
			String password = pw_et.getText().toString();
			if (TextUtils.isEmpty(phoneNumber)) {
				Toast.makeText(this, "请输入手机号码", Toast.LENGTH_SHORT).show();
			} else if (TextUtils.isEmpty(password)) {
				Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
			} else {
				// 登录请求
				loginReq(phoneNumber, password);
			}
			break;
		case R.id.forgetPw:// 忘记密码
			// 输入法消失
			((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
					.hideSoftInputFromWindow(LoginActivity.this
							.getCurrentFocus().getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);
			startActivity(new Intent(getApplicationContext(),
					ResetPwdActivity.class));

			break;
		default:
			break;
		}
	}

	/*
	 * 处理登陆请求
	 */
	private void loginReq(final String phoneNumber, final String password) {

		// 获得工作线程发来的消息
		final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				String s = (String) msg.obj;
				if ("ok".equals(s)) {
					// 显示内容
					// 获得状态码
					String status = (String) list.get(0).get("status");
					// 获得登录信息
					String message = (String) list.get(0).get("message");
					if ("10000".equals(status)) {// 登录成功
						// 保存验签值
						Constant.sigen = (String) list.get(0).get("sigen");
						// 账户名
						Constant.phone = userN_et.getText().toString();
						// 用户id
						Constant.userId = (String) list.get(0).get("userid");
						// 用户积分
						Constant.integral = (String) list.get(0)
								.get("integral");
						// 用户头像地址
						Constant.portrait = (String) list.get(0)
								.get("portrait");
						// 保存用户名和密码到 SharedPreferences
						SharedPreferences preference = getSharedPreferences(
								"person", Context.MODE_PRIVATE);
						Editor edit = preference.edit();
						edit.putString("phoneNumber", phoneNumber);
						edit.putString("password", password);
						edit.commit();
						startActivity(new Intent(getApplicationContext(),
								HomeTabActivity.class));
						AppManager.getAppManager().finishActivity();
						ph.setValue("sigen", Constant.sigen);
						ph.setValue(phoneNumber, Constant.sigen);
						ph.setValue("phoneNumber", "" + phoneNumber);
						ph.setValue("password", "" + password);
						ph.setValue("uid", "" + Constant.userId);
						ph.setValue("portrait", "" + Constant.portrait);
						ph.setValue("integral", "" + Constant.integral);
						Toast.makeText(LoginActivity.this, "登录成功", 1).show();
					} else if ("10001".equals(status)) {// 用户名或密码错误
						Toast.makeText(LoginActivity.this, message, 1).show();
					} else if ("10005".equals(status)) {// 系统问题
						Toast.makeText(LoginActivity.this, message, 1).show();
					}

					dialog.dismiss();
				} else if ("no".equals(s)) {// 网路问题
					dialog.dismiss();
					Toast.makeText(LoginActivity.this, "网络问题", 1).show();
				}
			}
		};
		dialog = ProgressDialog.show(this, null, "正在登录……请稍后", true, true);
		dialog.setCancelable(false);
		fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				HashMap<String, Object> map = null;
				try {
					String result = Net.login(Constant.login_url, "phone",
							phoneNumber, "password", password);
					// Log.i("返回", result);
					// 解析json字符串
					result = result.substring(1, result.length() - 1);
					Log.i("登录结果", result);
					JSONObject jsonObject = new JSONObject(result);
					map = new HashMap<String, Object>();
					// Log.i("结果", jsonObject.get("message")+"");
					// 登录结果信息
					map.put("message", jsonObject.getString("message"));
					// 状态码
					map.put("status", jsonObject.getString("status"));
					// 如果用户登录成功则将标识码和用户id存起来
					if ("10000".equals(jsonObject.getString("status"))) {
						// 保存用户头像
						Constant.userIc = Net.getBitmap(jsonObject
								.getString("portrait"));
						// 用户标识码
						map.put("sigen", jsonObject.getString("sigen"));
						// 上传头像用的key
						map.put("key", jsonObject.getString("key"));
						// 用户id
						map.put("userid", jsonObject.getString("userid"));
						// 总积分
						map.put("integral", jsonObject.getString("integral"));
						// 用户头像
						map.put("portrait", jsonObject.getString("portrait"));
					}
					list.add(map);
					// 通知UI
					Message msg = new Message();
					msg.obj = "ok";
					handler.sendMessage(msg);
				} catch (Exception e) {
					e.printStackTrace();
					// 通知UI
					Message msg = new Message();
					msg.obj = "no";
					handler.sendMessage(msg);
				}
			}
		});
	}
}

 

一套非常标准的数据请求和数据展示代码的模板

标签:

原文地址:http://www.cnblogs.com/childhooding/p/4470867.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!