服务器数据交互

  • o6_856059
    了解作者
  • 13.1KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-12 02:50
    上传日期
和服务器的数据交互
android数据交互.rar
  • android数据交互
  • background_login.xml
    222B
  • login.xml
    4.4KB
  • ServiceConnectionSE.java
    1.4KB
  • MyDBHelper.java
    3.9KB
  • blue_button.9.png
    3.3KB
  • SearchDate.java
    667B
  • UserLogin.java
    5.9KB
  • HttpUtil.java
    3.1KB
  • WSHelper.java
    1.4KB
  • MyAndroidHttpTransport.java
    734B
  • ParseJson.java
    421B
  • HttpFileUpTool.java
    3.2KB
内容介绍
package com.stxx.cssm.login; import org.json.JSONException; import org.json.JSONObject; 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; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import com.stxx.cssm.R; import com.stxx.cssm.db.MyDBHelper; import com.stxx.cssm.main.TabControl; import com.stxx.cssm.util.MessageBox; import com.stxx.cssm.util.MyApplication; public class UserLogin extends Activity implements OnClickListener { private ProgressDialog progressDialog = null; private EditText username_edit; private EditText password_edit; private Button login_signin_btn; private Button login_cancel_btn; private int isLogin = 0; private String username = ""; private String password = ""; private String userid = ""; private String empid = ""; private Context ctx; private SharedPreferences sp; private Editor editor; // private Spinner sp_login_fwq; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); MyApplication.getInstance().addActivity(this);//手动管理activity ctx = UserLogin.this; sp = ctx.getSharedPreferences("CSSMData", MODE_PRIVATE); editor = sp.edit(); login_signin_btn = (Button) findViewById(R.id.login_signin_btn); login_signin_btn.setOnClickListener(this); login_cancel_btn = (Button) findViewById(R.id.login_cancel_btn); login_cancel_btn.setOnClickListener(this); username_edit = (EditText) findViewById(R.id.username_edit); password_edit = (EditText) findViewById(R.id.password_edit); // sp_fwq_Show(); // 测试数据 username_edit.setText(getData("username", ""));// 取出用户名 // username_edit.setText("admin"); // password_edit.setText("111111"); } @Override public void onClick(View v) { if (v == login_cancel_btn) { MyApplication.getInstance().exit(); } if (v == login_signin_btn) { setListener();// 登录线程 } } /** * 手机按钮监听 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // 如果是返回键,则绑定返回按钮 if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME) { MyApplication.getInstance().exit();//关闭程序 } return super.onKeyDown(keyCode, event); } /** * 用户名密码验证成功后,存储当前登录用户并作出跳转 */ private void move() { // // 存储登录用户信息 // UserVo userVo = new UserVo(); // userVo.setEmpid(empid); // userVo.setUserid(userid); // userVo.setUserName(username); // userVo.setPassword(password); // GlobalVar.getInstance().setUserVo(userVo); saveData("empid", empid); saveData("userid", userid); saveData("username", username); saveData("password", password); // 跳转 Intent intent = new Intent(); intent.setClass(UserLogin.this, TabControl.class); UserLogin.this.startActivity(intent); UserLogin.this.finish(); } /** * 将需要存储的数据存入临时文件STRING * * @param data_Key * 存储临时文件key * @param data_value * 存储临时文件value */ public void saveData(String data_Key, String data_value) { // 存入数据 editor.putString(data_Key, data_value); editor.commit(); } /** * 获取临时文件中存储的值String * * @param data_Key * 临时文件获取key * @param data_default * 临时文件获取失败时默认值 * @return 获取临时文件String 类型 */ public String getData(String data_Key, String data_default) { return sp.getString(data_Key, data_default); } /** * 用Handler来更新UI */ private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // 关闭ProgressDialog progressDialog.dismiss(); switch (isLogin) { case 0: MessageBox.createAlertDialog("系统提示", "用户名或密码不能为空!", "返回", UserLogin.this).show(); break; case 1: MessageBox.createAlertDialog("系统提示", "登录失败,请重试!", "返回", UserLogin.this).show(); break; case 2: move(); break; default: break; } } }; /** * 点击按钮事件listener */ private void setListener() { // 显示ProgressDialog progressDialog = ProgressDialog.show(UserLogin.this, "系统提示:", "正在登录...", true, false); // 新建线程 new Thread() { @Override public void run() { try { checkUserLogin(); // isLogin = 2;// 设置登录成功,测试 // Thread.sleep(3000);// 线程终止 } catch (Exception e) { isLogin = 1;// 验证失败 e.printStackTrace(); } // 向handler发消息 handler.sendEmptyMessage(0); } }.start(); } private void checkUserLogin() throws JSONException { username = username_edit.getText().toString().trim(); password = password_edit.getText().toString().trim(); if (username.equals("") || username == null || username.equals("null") || password.equals("") || password == null || password.equals("null")) { isLogin = 0;// 设置登录失败 } else { // 调用接口检测 String url = "obstacleOrder/plogin.action?username=" + username + "&password=" + password; // 调用数据链接并获取返回值 String json = MyDBHelper.getInstances().queryStringForPost(url); JSONObject jsonOb = new JSONObject(json).getJSONObject("result"); int isOK = jsonOb.getInt("flag"); if (isOK == 0) { isLogin = 1;// 验证失败 } else { //SharedPreferences存储用户名和部门编码 userid = jsonOb.getString("userid"); empid = jsonOb.getString("empid"); isLogin = 2;// 设置登录成功 } } } }
评论
    相关推荐