现在的位置: 首页 > 综合 > 正文

android 登陆、注册、并个指定用户充值

2012年09月09日 ⁄ 综合 ⁄ 共 12935字 ⁄ 字号 评论关闭

首发地址:

android注册 登录+修改帐号密码+添加资料+给指定帐号充值 .....
http://www.eoeandroid.com/thread-225709-1-1.html

仿UCWEB界面源码!
http://www.eoeandroid.com/thread-225509-1-1.html

最近公司让写个android注册 登录 修改帐号密码 添加资料 给新注册用户充值DEMO  现在功能都已经OK  目前只剩下一些小细节  现在我就把源码发布出来 给一些需要的人参考,在这里重点只讲怎么去请求服务器 和服务器返回的一些什么东西给我们 我们如何拿到 如何处理

有图有真相(嘎嘎):

 

   

  

  

关于接口的类:

public class UserDataServiceHelper {
    /** 时间戳 */
    static long serial = new Date().getTime();
 
    /** KEY */
    static String key = "后台人员会给你个密钥";
 
    /** 网站地址 */
    private static final String HOST_IP = "你的请求地址";
 
    /** 请求服务器 S1,S2,S3等 */
    static String server = "s1";
 
    /** 返回码 1=成功 */
    public static String results = null;
 
    /** 登录UID */
    public static String LoginUid = null;
 
    /** 注册UID */
    public static String RegisterUid = null;
 
    /** 快速注册根据返回得到用户名 */
    public static String FastUserName = null;
 
    /** 快速注册根据返回得到密码 */
    public static String FastPassWord = null;
 
    /** 快速注册根据返回UID */
    public static String Uid = null;
     
     
    /** 充值请求地址*/
    private  static final String HOST_IP_CREDIT = "你的充值请求地址";
     
 
    // 注册
    public static boolean Register(Context context, String action, String username,
            String password, String from) {
        Log.i("TAG", "得到的" + context + action + username + password + from);
        try {
            HttpClient httpClient = new DefaultHttpClient();
            // 请求超时
            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
 
            // 读取超时
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
            StringBuilder uri = new StringBuilder();
            uri.append(HOST_IP + "XXXXXX" + "?" + "action=" + action);
            uri.append("&username=");
            uri.append(username);
            uri.append("&password=");
            uri.append(password);
            uri.append("&from=");
            uri.append(from);
            uri.append("&server=");
            uri.append(server);
            uri.append("&apitime=");
            uri.append(serial);
            uri.append("&key=");
            uri.append(getMD5Str(serial + key));
            Log.i("TAG", "请求地址:" + uri.toString());
            HttpPost httpPost = new HttpPost(uri.toString());
            HttpResponse httpResponse = httpClient.execute(httpPost);
            String jsonforString = null;
            String result = null;
            String result2 = null;
            String result3 = null;
            String result4 = null;
            String result5 = null;
            String result6 = null;
            String result7 = null;
            // 返回json报文
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                jsonforString = EntityUtils.toString(httpResponse.getEntity());
                JSONObject jsonObject = new JSONObject(jsonforString);
                Log.i("TAG", "JSONObject对象=" + jsonforString);
                result = jsonObject.getString("result");
                result2 = jsonObject.getString("msg");
                Log.i("TAG", "result2:返回的错误信息"+result2);
                Log.i("TAG", "成功还是失败返回" + result);
                if (httpClient != null) {
                    httpClient.getConnectionManager().shutdown();
                }
                if (result.equalsIgnoreCase("1")) {
                    return true;
                } else {
                    return false;
                }
                 
                 
                 
                 
                 
            }
        } catch (ConnectException e) {
            Toast.makeText(context, "网络连接异常", Toast.LENGTH_LONG).show();
        } catch (SocketTimeoutException e) {
            Toast.makeText(context, "连接超时", Toast.LENGTH_LONG).show();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    // 登录
    public static boolean logins(Context context, String action, String username, String password,
            String from) {
        Log.i("TAG", "得到的" + context + action + username + password);
        try {
            HttpClient httpClient = new DefaultHttpClient();
            // 请求超时
            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
 
            // 读取超时
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
            StringBuilder uri = new StringBuilder();
            uri.append(HOST_IP + "XXXXXXXX" + "?" + "action=" + action);
            uri.append("&username=");
            uri.append(username);
            uri.append("&password=");
            uri.append(password);
            uri.append("&from=");
            uri.append(from);
            uri.append("&server=");
            uri.append(server);
            uri.append("&apitime=");
            uri.append(serial);
            uri.append("&key=");
            uri.append(getMD5Str(serial + key));
            Log.i("TAG", "请求地址:" + uri.toString());
            HttpPost httpPost = new HttpPost(uri.toString());
            HttpResponse httpResponse = httpClient.execute(httpPost);
            String jsonforString = null;
            String result = null;
            String login = null;
            // 返回json报文
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                jsonforString = EntityUtils.toString(httpResponse.getEntity());
                JSONObject jsonObject = new JSONObject(jsonforString);
                Log.i("TAG", "JSONObject对象=" + jsonforString);
                result = jsonObject.getString("result");
                login = jsonObject.getString("uid");
                Log.i("TAG", "成功还是失败返回" + result);
                if (httpClient != null) {
                    httpClient.getConnectionManager().shutdown();
                }
                if (result.equalsIgnoreCase("1")) {
                    Log.i("TAG", "服务器返回码" + result);
                    results = result;
                    LoginUid = login;
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ConnectException e) {
            Toast.makeText(context, "网络连接异常", Toast.LENGTH_LONG).show();
        } catch (SocketTimeoutException e) {
            Toast.makeText(context, "连接超时", Toast.LENGTH_LONG).show();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    // 快速注册
    public static boolean fast(Context context, String action, String from) {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            // 请求超时
            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
 
            // 读取超时
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
            StringBuilder uri = new StringBuilder();
            uri.append(HOST_IP + "XXXXXXXX" + "?" + "action=" + action);
            uri.append("&from=");
            uri.append(from);
            uri.append("&server=");
            uri.append(server);
            uri.append("&apitime=");
            uri.append(serial);
            uri.append("&key=");
            uri.append(getMD5Str(serial + key));
            Log.i("TAG", "快速注册请求地址:" + uri.toString());
            HttpPost httpPost = new HttpPost(uri.toString());
            HttpResponse httpResponse = httpClient.execute(httpPost);
            String jsonforString = null;
            String result = null;
            String UserNames = null;
            String PassWords = null;
            String UserId = null;
            // 返回json报文
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                jsonforString = EntityUtils.toString(httpResponse.getEntity());
                JSONObject jsonObject = new JSONObject(jsonforString);
                Log.i("TAG", "JSONObject对象=" + jsonforString);
                result = jsonObject.getString("result");
                Log.i("TAG", "快速注册成功还是失败返回码" + result);
                UserNames = jsonObject.getString("username");
                PassWords = jsonObject.getString("password");
                Log.i("TAG", "快速注册成功返回的密码" + PassWords);
                Log.i("TAG", "快速注册成功返回用户名" + UserNames);
                UserId = jsonObject.getString("uid");
                if (httpClient != null) {
                    httpClient.getConnectionManager().shutdown();
                }
                if (result.equalsIgnoreCase("1")) {
                    Log.i("TAG", "快速注册服务器返回码" + result);
                    results = result;
                    FastUserName = UserNames;
                    FastPassWord = PassWords;
                    Uid = UserId;
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ConnectException e) {
            Toast.makeText(context, "网络连接异常", Toast.LENGTH_LONG).show();
        } catch (SocketTimeoutException e) {
            Toast.makeText(context, "连接超时", Toast.LENGTH_LONG).show();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
    // 修改帐号
    public static boolean updateData(Context context, String action, String uid, String username,
            String password, String newname, String newpass) {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            // 请求超时
            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
 
            // 读取超时
            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
            StringBuilder uri = new StringBuilder();
            uri.append(HOST_IP + "XXXXXXXXX" + "?" + "action=" + action);
            uri.append("&uid=");
            uri.append(uid);
            uri.append("&username=");
            uri.append(username);
            uri.append("&password=");
            uri.append(password);
            uri.append("&newname=");
            uri.append(newname);
            uri.append("&newpass=");
            uri.append(newpass);
            uri.append("&server=");
            uri.append(server);
            uri.append("&apitime=");
            uri.append(serial);
            uri.append("&key=");
            uri.append(getMD5Str(serial + key));
            Log.i("TAG", "提交更新:" + uri.toString());
            HttpPost httpPost = new HttpPost(uri.toString());
            HttpResponse httpResponse = httpClient.execute(httpPost);
            String jsonforString = null;
            String result = null;
            String UserNames = null;
            String PassWords = null;
            String UserId = null;
            // 返回json报文
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                jsonforString = EntityUtils.toString(httpResponse.getEntity());
                JSONObject jsonObject = new JSONObject(jsonforString);
                // Log.i("TAG", "JSONObject对象=" + jsonforString);
                result = jsonObject.getString("result");
                Log.i("TAG", "提交更新返回码" + result);
                // UserNames = jsonObject.getString("username");
                // PassWords = jsonObject.getString("password");
                // Log.i("TAG", "快速注册成功返回的密码" + PassWords);
                // Log.i("TAG", "快速注册成功返回用户名" + UserNames);
                UserId = jsonObject.getString("uid");
                if (httpClient != null) {
                    httpClient.getConnectionManager().shutdown();
                }
                if (result.equalsIgnoreCase("1")) {
                    Log.i("TAG", "提交更新" + result);
                    // results = result;
                    // FastUserName = UserNames;
                    // FastPassWord = PassWords;
                    // Uid = UserId;
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ConnectException e) {
            Toast.makeText(context, "网络连接异常", Toast.LENGTH_LONG).show();
        } catch (SocketTimeoutException e) {
            Toast.makeText(context, "连接超时", Toast.LENGTH_LONG).show();
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
 
     
 
    // MD5
    public static String getMD5Str(String str) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            messageDigest.update(str.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
 
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
 
        byte[] byteArray = messageDigest.digest();
        StringBuffer md5StrBuff = new StringBuffer();
        for (int i = 0; i < byteArray.length; i++) {
            if (Integer.toHexString(0xFF & byteArray<i>).length() == 1) {
                md5StrBuff.append("0").append(
                        Integer.toHexString(0xFF & byteArray<i>));
            } else {
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray<i>));
            }
        }
        return md5StrBuff.toString();
    }
 
 
}

 

首先我们先看一下请求服务器的类 如何写的

我们首先看注册那边返回的JSON都是什么内容吧(一会我会讲到如何抓到result>1的错误信息,然后返回给activity显示出来)  一般你们公司都会有个后台给你们写个接口文档扔给你 让你去做的

{ “result”: 1, “uid”:22771,”msg”:””}
 
result=1 注册成功, uid为玩家uid, msg为空
 
result>1时注册失败, 此时返回的 uid=0
result=2:  msg:用户名格式不对 
result=3:  msg:此用户名已经被注册
result=4:  msg:密码格式不对(长度不是6-16位或者包含了其他字符,比如中文标点之类)
result=5:  msg:广告来源为空 from 的值不允许为空 
result=6:  msg:系统维护,此时不允许注册
result>6 时为其他错误, msg会返回错误的具体原因

我们在注册页面去请求服务器:

在我的工程:Register类

boolean flag ;//条件成立跳转到登陆界面
    /** 请求服务器 */
        if (userName != null || password != null || from != null) {
            flag = UserDataServiceHelper.Register(context, "reg", userName, password, from);
            if(flag){
                Intent intent = new Intent();
                intent.putExtra("name", userName);
                intent.putExtra("pw", password);
                intent.putExtra("fm",from);
                intent.setClass(Register.this, Login.class);
                startActivity(intent);
            }else {
                Log.i("TAG", "不成立");
            }
            Log.i("TAG", "请求服务器" + userName + password + from);
        }

传的参数里面 第一个就是context  第二个就是一个注册的参数(你们后台都会有自己弄一个参数来区分的),第三个参数就是你的名字 第4个参数就是你的密码 第五个参数其实就是一个渠道的意思(从那个渠道过来注册的,比如你从googlePlay注册的 这里随便定义一个参数 让你们的老大知道这个从googlePlay下载注册的,现在产品都这样搞的)
如果注册接口成功返回true 那么flag就会是true(默认是false嘛)  就去执行Intent,然后putExtra 把需要的东西传值到登录界面

登录界面会做什么事呢? 接着上面的问题 看下面的代码

 

  /** 初始化注册VIEW组件 */
    private void findViewById() {
        view_userName = (EditText) findViewById(R.id.loginUserNameEdit);
        view_password = (EditText) findViewById(R.id.loginPasswordEdit);
        view_rememberMe = (CheckBox) findViewById(R.id.loginRememberMeCheckBox);
        view_loginSubmit = (Button) findViewById(R.id.loginSubmit);
        view_loginRegister = (Button) findViewById(R.id.loginRegister);
        view_fast = (TextView) findViewById(R.id.fast);
 
        /** 注册成功后传过来用户名和密码,显示在登录界面 */
        if (!flag) {
            Intent intent = getIntent();
            userName = intent.getStringExtra("name");
            password = intent.getStringExtra("pw");
            from = intent.getStringExtra("fm");
            view_rememberMe.setChecked(false);//小BUG
            view_userName.setText(userName);
            view_password.setText(password);
        }
 
    }

 

登录界面就会用getStringExtra方法把刚刚从注册传过来的值  这里是根据Key,value  我们只要得到这个key("name")就OK了。  
然后我们在用setText 显示在editText上!

 

 

view_userName.setText(userName);
            view_password.setText(password);

 

然后我们在点击登录的时候看代码 如何把刚刚从注册传过来的值在去传到登录接口 其实在这里很简单 在赋值给的string就OK了 赋值好后然后在把赋值的值传到登录接口 看例子:

 

 

  /** 登录Button Listener */
    private OnClickListener submitListener = new OnClickListener() {
 
        @Override
        public void onClick(View v) {
            Log.i("TAG", "submitListener");
            proDialog = ProgressDialog.show(Login.this, "连接中..",
                    "连接中..请稍后....", true, true);
            // 开启一个线程进行登录验证,主要是用户失败成功可以直接通过startAcitivity(Intent)转向
            Thread loginThread = new Thread(new LoginFailureHandler());
            loginThread.start();// 开启
 
        }
    };

 

// 另起一个线程登录
    class LoginFailureHandler implements Runnable {
        @Override
        public void run() {
            userName = view_userName.getText().toString();
            Log.i("TAG", "userName LoginFailureHandler" + userName);
            password = view_password.getText().toString();
            Log.i("TAG", "password LoginFailureHandler" + password);
 
            /** 请求服务器 */
            if (userName != null || password != null) {
                boolean loginState = UserDataServiceHelper.logins(context, "login", userName,
                        password, from);
 
                Log.i("TAG", "登录返回条件" + loginState);
                // 登录成功
                if (loginState) {
                    String LoginUerId = UserDataService.LoginUid;
                    // 需要传输数据到登陆后的界面,
                    Intent intent = new Intent();
                    intent.setClass(Login.this, IndexPage.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("LOGIN_USERNAME", userName);
                    bundle.putString("LOGIN_PASSWORD", password);
                    bundle.putString("LOGIN_ID", LoginUerId);
                    intent.putExtras(bundle);
                    // 转向登陆后的页面
                    startActivity(intent);
                    // /** 得到请求服务器返回码 */
                    String loginStateInt = UserDataService.results;
                    int Less = Integer.valueOf(loginStateInt); // 转换成整形
                    Log.i("TAG", "登录后的返回码:" + Less);
                    if (Less == 1) {
                        StatusCode = true;
                         
                    }
 
                    // 登录成功记住帐号密码
                    if (StatusCode) {
                        if (isRememberMe()) {
                            saveSharePreferences(true, true);
                        } else {
                            saveSharePreferences(true, false);
                        }
 
                    } else {
                        // 如果不是网络错误
                        if (!isNetError) {
                            clearSharePassword();
                            clearShareName();
 
                        }
 
                    }
                    if (!view_rememberMe.isChecked()) {
                        clearSharePassword();
                        clearShareName();
                    }
                    proDialog.dismiss();
                } else {
                    // 通过调用handler来通知UI主线程更新UI,
                    Log.i("TAG", "连接失败");
                    Message message = new Message();
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("isNetError", isNetError);
                    message.setData(bundle);
                    loginHandler.sendMessage(message);
                }
 
            }
 
        }
    }

登录后 我们就会看到一个登录成功的页面,下面有4个按钮 修改帐号,添加资料,从设密码

那么修改帐号的流程 还是跟上面登录注册一样  把所有的值用putExtra方法传过来  然后在修改帐号页面getStringExtra在得到("name",passwor)等  ,在从新声明一个string类型  在赋值  在去请求  修改帐号密码接口

添加资料也就是去请求地址 然后里面需要传什么东西 就传什么过去。

从设置也是和上面的流程 putExtra-----getStringExtra("name",passwor)等 在从新声明一个string类型  在赋值  在去请求
其实注册 登录  修改帐号密码  和添加资料 都是很简单的  无非就是putExtra   getStringExtra  声明   赋值   请求  返回信息   显示
备注:
附件里面我把接口那部分已经删掉了   你们可以看上面我写的请求服务器的类  根据自己的业务需求来添加和删除   请求服务器类写的一个不好的地方就是 每个方法都加了static  呵呵   这个原因主要是  我写了一个方法  然后剩下的接口我就复制上面的 然后改改。

 源码:andoird96pk.rar

 

抱歉!评论已关闭.