package com.fuda.activity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import org.apache.http.HttpEntity;
import org.apache.http.client.CookieStore;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.jarjar.apache.commons.codec.digest.DigestUtils;
import org.json.JSONObject; import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.app.Activity;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.text.Editable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast; import com.fuda.AppContext;
import com.fuda.R;
import com.fuda.Unity;
import com.fuda.model.StudentInfoModel;
import com.fuda.ui.MessageBar;
import com.fuda.ui.MessageBar.OnMessageClickListener;
import com.fuda.ui.MyCommonUtils;
import com.fuda.ui.MyDialog;
import com.fuda.ui.SearchTextView;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.student.utils.DateDeserializer; public class LoginActivity extends FinalActivity { @ViewInject(id = R.id.account)
private SearchTextView accountV;
@ViewInject(id = R.id.password)
private SearchTextView passwordV;
@ViewInject(id = R.id.login_btn)
private Button login_btn;
private CookieStore cookieStore;
private MyAsyncTask myAsyncTask;
private Dialog dialog;
private SharedPreferences p;
private MessageBar mMessageBar; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_login); initView();
} OnMessageClickListener messageListener = new OnMessageClickListener() {
@Override
public void onMessageClick(Parcelable token) {
// TODO Auto-generated method stub
}
}; public void showMessage(String message) {
mMessageBar.show(message, "关闭", R.drawable.ic_messagebar_undo, new Bundle());
} private void initView() {
mMessageBar = new MessageBar(this);
mMessageBar.setOnClickListener(messageListener);
login_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String account = accountV.getText().toString();
String password = passwordV.getText().toString();
if (account.equals("")) {
showMessage(getResources().getString(R.string.gd_login_username_empty_label));
return;
} else if (password.equals("")) {
showMessage(getResources().getString(R.string.gd_login_password_empty_label));
return;
}
//安全性判断
if (myAsyncTask != null && myAsyncTask.getStatus() != AsyncTask.Status.FINISHED)
myAsyncTask.cancel(true);
myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();
}
});
} class MyAsyncTask extends AsyncTask<Object, Void, Object> {
@Override
protected void onPreExecute() {
Message msg = new Message();
msg.what = 2;
uiHandler.sendMessage(msg);
super.onPreExecute();
} @Override
protected Object doInBackground(Object... arg0) {
Object obj = null;
if (!isCancelled()) {
FinalHttp finalHttp = new FinalHttp(getBaseContext());
AjaxParams ap = new AjaxParams();
StringBuffer buffer = new StringBuffer();
//把所需的参数集中起来
buffer.append("{\"LoginName\":\"" + accountV.getText().toString() + "\",\"Password\":\"" + new DigestUtils().sha512Hex(passwordV.getText().toString())
+ "\",\"UniversityCode\":\"10386\",\"LoginWay\":\"Number\"}");
ap.put("data", buffer.toString());
cookieStore = new BasicCookieStore();
finalHttp.configCookieStore(cookieStore);
obj = finalHttp.postSync(Unity.loginAPI, ap);
}
return obj;
} //doInBackground执行之后的结果obj就是onPostExecute的参数
@Override
protected void onPostExecute(Object result) {
Message msg = new Message();
msg.obj = result;
msg.what = 1;
uiHandler.sendMessage(msg);
super.onPostExecute(result);
} @Override
protected void onCancelled() {
super.onCancelled();
} private Handler uiHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
if (dialog != null) dialog.dismiss();
try {
//把字符串转成json
JSONObject jo = new JSONObject(msg.obj.toString());
//返回其中的Success字段
String flag = jo.get("Success").toString();
if ("true".equals(flag)) {
// p.edit().putString("account", accountV.getText().toString()).commit();
// p.edit().putString("password", passwordV.getText().toString()).commit(); //http://blog.csdn.net/xinhu_li0104/article/details/8700371
Gson gson = new GsonBuilder().create();
//讲json字符串,转化成StudentInfoModel类型的对象
StudentInfoModel studentInfoModel = gson.fromJson(jo.optString("ReturnObj").toString(), StudentInfoModel.class);
//保存cookie和用户信息
getContext().setCookieStore(cookieStore);
getContext().setStudentInfoModel(studentInfoModel);
Intent intent = new Intent(LoginActivity.this, HomeTabHostActivity.class);
intent.putExtra("studentInfoModel", studentInfoModel);
startActivity(intent);
} else {
showMessage(jo.optString("Msg"));
}
} catch (Exception e) {
showMessage(getResources().getString(R.string.forecast_error_check_hint));
e.printStackTrace();
}
break;
case 2:
dialog=new MyDialog(LoginActivity.this).showProgressDialog(null, myAsyncTask);
break;
default:
break;
}
super.handleMessage(msg);
}
};
} public AppContext getContext() {
return ((AppContext) super.getApplicationContext());
} @Override
protected void onDestroy() {
if (myAsyncTask != null && myAsyncTask.getStatus() != AsyncTask.Status.FINISHED)
myAsyncTask.cancel(true);
super.onDestroy();
} @Override
protected void onResume() {
super.onResume();
// String account = null;
// String password = null;
// p = getSharedPreferences("userinfo", MODE_WORLD_READABLE);
// account = p.getString("account", "");
// password = p.getString("password", "");
// if (account != "" && password != "") {
// accountV.setText(account);
// passwordV.setText(password);
// }
} @Override
protected void onStart() {
super.onStart();
} }

android笔记---LoginActivity extends FinalActivity的更多相关文章

  1. Android笔记——Android中数据的存储方式(二)

    我们在实际开发中,有的时候需要储存或者备份比较复杂的数据.这些数据的特点是,内容多.结构大,比如短信备份等.我们知道SharedPreferences和Files(文本文件)储存这种数据会非常的没有效 ...

  2. Android笔记:触摸事件的分析与总结----TouchEvent处理机制

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://glblong.blog.51cto.com/3058613/1559320   ...

  3. Android笔记--View绘制流程源码分析(二)

    Android笔记--View绘制流程源码分析二 通过上一篇View绘制流程源码分析一可以知晓整个绘制流程之前,在activity启动过程中: Window的建立(activit.attach生成), ...

  4. Android笔记--自定义控件仿遥控器的圆形上下左右OK圆盘按钮

    原文:Android笔记--自定义控件仿遥控器的圆形上下左右OK圆盘按钮 上面就是几张预览图!代码在最底下 主要就两个步骤,画图.监听点击 1.整个控件基本上是一步步画出来的,重写onDraw方法开始 ...

  5. Android笔记(四) Activity之间的数据传递

    我们之前使用Intent进行Activity之间的跳转,其实Intent还可以在启动活动的时候传递数据. Intent提供了一系列的putExtra方法以便我们把想要传递的数据暂存在Intent中,待 ...

  6. Android 笔记之 R 文件

    Android笔记之R文件 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: red; te ...

  7. Android 笔记之 Android 系统架构

    Android笔记之Android系统架构 h2{ color: #4abcde; } a{ color: blue; text-decoration: none; } a:hover{ color: ...

  8. Android笔记之使用Glide加载网络图片、下载图片

    Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...

  9. Android笔记--View绘制流程源码分析(一)

    Android笔记--View绘制流程源码分析 View绘制之前框架流程分析 View绘制的分析始终是离不开Activity及其内部的Window的.在Activity的源码启动流程中,一并包含 着A ...

随机推荐

  1. GitLab概念——Group、Project、Member

    概念说明: Group是一个父子结构的目录 Group每一级都可以设置关联的Member,同时每一级下都可以创建项目 Group关联的Member和Member对应的权限,会继承到Group下的所有P ...

  2. 使用MSTSC远程登录时提示证书无效的解决方法

    On your local machine Open Windows command prompt type: gpedit.msc -> Press Enter -> a new win ...

  3. IOS中UITableview中封装九宫格

    第一步引入SecondNav目录即可 第二步引入头文件 #import "DIYTableView.h" #import "invoiceInfo.h" 实现协 ...

  4. Kafka 如何读取指定topic中的offset -------------用来验证分区是不是均衡!!!(__consumer_offsets)(已验证!)

    我现在使用的是librdkafka 的C/C++ 的客户端来生产消息,用flume来辅助处理异常的数据,,, 但是在前段时间,单独使用flume测试的时候发现,flume不能对分区进行负载均衡!同一个 ...

  5. springAOP记录用户操作日志

    项目已经开发完成,需要加用户操作日志,如果返回去加也不太现实,所以使用springAOP来完成比较合适. 注解工具类: @Retention(RetentionPolicy.RUNTIME) @Tar ...

  6. DMA直接内存存取原理

    DMA是指外部设备不通过CPU而直接与系统内存交换数据的接口技术. 要把外设的数据读入内存或把内存的数据传送到外设,一般都要通过CPU控制完成,如CPU程序查询或中断方式.利用中断进行数据传送,可以大 ...

  7. mac使用phpize进行安装的时候碰到的问题

    问题: grep: /usr/include/php/main/php.h: No such file or directory grep: /usr/include/php/Zend/zend_mo ...

  8. Android事件总线还能怎么玩?

    作者简介:何红辉,Android工程师,现任职于友盟. 顾名思义,AndroidEventBus是一个Android平台的事件总线框架,它简化了Activity.Fragment.Service等组件 ...

  9. 【SqlServer】在SqlServer中把数据导入导出为Excel文件

    这里笔者介绍利用SqlServer数据库操作EXECEL文件. 1.将Excel表中的数据导入为SqlServer数据库 把Excel表中的数据导入为SqlServer数据库中的数据. 新建一个Exc ...

  10. Spring Boot加载配置文件

    问题1:Spring如何加载配置,配置文件位置? 1.默认位置: Spring Boot默认的配置文件名称为application.properties,SpringApplication将从以下位置 ...