• android笔记---LoginActivity extends FinalActivity


    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();
        }
    
    
    }
  • 相关阅读:
    FFT 优化和任意模数 FFT
    构造 DFT :某少女附中的体育课 题解
    多项式操作
    String主要方法
    我的第一篇java笔记
    个人作业——软件评测
    软件工程实践2019第五次作业
    软件工程实践2019第四次作业
    软件工程实践2019第三次作业
    软件工程实践2019第二次作业
  • 原文地址:https://www.cnblogs.com/newlist/p/3298263.html
Copyright © 2020-2023  润新知