android注册 登录+修改帐号密码+添加资料+给指定帐号充值

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

接口的类 在一楼  因为我写的东西太多了 文字有限制
有图有真相:
device-2012-11-05-212526.png 


device-2012-11-05-212729.png 


device-2012-11-05-212758.png 


device-2012-11-05-212923.png 


device-2012-11-05-212938.png 



device-2012-11-05-212948.png 

首先我们先看一下请求服务器的类 如何写的 可结合我上一篇:
http://www.eoeandroid.com/thread-212252-1-1.html    
这里就完善上一篇的内容:


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

?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
{ “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类


?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
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 把需要的东西传值到登录界面

登录界面会做什么事呢? 接着上面的问题 看下面的 代码
?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
   /** 初始化注册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上!
?
代码片段,双击复制
01
02
view_userName.setText(userName);
             view_password.setText(password);




然后我们在点击登录的时候看代码  如何把刚刚从注册传过来的值在去传到登录接口  其实在这里很简单 在赋值给的string就OK了 赋值好后然后在把赋值的值传到登录接口 看例子:
?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
   /** 登录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(); // 开启
 
         }
    };


?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// 另起一个线程登录
    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 (141.9 KB, 下载次数: 419) 

代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
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();
    }
 
 
}
</i></i></i>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值