一、明日计划
完成书籍上传与信息保存的数据库交互,开始设计书籍下载
二、今日成果
成功实现登录注册的整合,完成了在云端数据库的存储
package Servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.servletexception;import javax.servlet.http.httpServlet;import javax.servlet.http.httpServletRequest;import javax.servlet.http.httpServletResponse;import dao.dao;import net.sf.Json.JsONException;import net.sf.Json.JsONObject;public class LoginServlet extends httpServlet { protected voID doGet(httpServletRequest request, httpServletResponse response) throws servletexception, IOException { request.setCharacterEnCoding("utf-8"); response.setCharacterEnCoding("utf-8"); response.setContentType("text/HTML; charset=utf-8"); String username=request.getParameter("username"); String password=request.getParameter("password"); dao dao =new dao(); PrintWriter out = response.getWriter(); JsONObject Json=new JsONObject(); if(!dao.isExist(username)) { Json.put("result", 0); }else { boolean result=dao.Login(username, password); if(result) { Json.put("result", 1); }else { Json.put("result", 2); } } out.print(Json); return; } protected voID doPost(httpServletRequest request, httpServletResponse response) throws servletexception, IOException { // Todo auto-generated method stub doGet(request, response); }}LoginServlet.java
package Servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.servletexception;import javax.servlet.http.httpServlet;import javax.servlet.http.httpServletRequest;import javax.servlet.http.httpServletResponse;import dao.dao;import entity.User;import net.sf.Json.JsONObject;public class RegisterServlet extends httpServlet { protected voID doGet(httpServletRequest request, httpServletResponse response) throws servletexception, IOException { request.setCharacterEnCoding("utf-8"); response.setCharacterEnCoding("utf-8"); response.setContentType("text/HTML; charset=utf-8"); String username=request.getParameter("username"); String password=request.getParameter("password"); User user=new User(username,password); dao dao =new dao(); PrintWriter out = response.getWriter(); JsONObject Json=new JsONObject(); boolean result1=dao.isExist(username); if(!result1) { boolean result2=dao.Register(user); if(result2) { Json.put("result", 1); }else { Json.put("result", 2); } }else { Json.put("result", 0); } out.print(Json); return; } protected voID doPost(httpServletRequest request, httpServletResponse response) throws servletexception, IOException { // Todo auto-generated method stub doGet(request, response); }}RegisterServlet.java
package com.example.fooks;import androID.app.Activity;import androID.content.Context;import androID.content.Intent;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Toast;import androIDx.annotation.Nullable;import com.example.fooks.entity.User;import com.loopj.androID.http.AsynchttpClIEnt;import com.loopj.androID.http.AsynchttpResponseHandler;import com.loopj.androID.http.RequestParams;import org.Json.JsONException;import org.Json.JsONObject;import java.io.UnsupportedEnCodingException;import cz.msebera.androID.httpclIEnt.header;public class LoginActivity extends Activity { private EditText mUsername; private EditText mPassword; private button mLogin; private button mGotoregister; private String username; private String password; private String TAG="LoginActivity"; public static voID start(Context context) { Intent intent = new Intent(context,LoginActivity.class); context.startActivity(intent); } @OverrIDe protected voID onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_login); //初始化控件 initVIEw(); //设置点击事件 initListener(); } private voID initListener() { mGotoregister.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Intent intent =new Intent(LoginActivity.this, Registeractivity.class); startActivity(intent); } }); mLogin.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { username=mUsername.getText().toString().trim(); password=mPassword.getText().toString().trim(); if(username.isEmpty()){ Toast.makeText(getApplicationContext(),"请输入账号",Toast.LENGTH_LONG).show(); }else if(password.isEmpty()){ Toast.makeText(getApplicationContext(),"请输入密码",Toast.LENGTH_LONG).show(); }else { Login(); } } }); } protected voID Login() { User user =new User(); user.setUsername(username); user.setPassword(password); Log.e(TAG,"用户名"+user.getUsername()); Log.e(TAG,"密码"+user.getpassword()); AsynchttpClIEnt clIEnt = new AsynchttpClIEnt();//实例化上传对象 String url = "http://47.94.229.72:8080/Fooks/LoginServlet";//url组成:ip:端口 + 服务端工程名 + servlet名 RequestParams params = new RequestParams(); params.put("username",user.getUsername()); params.put("password",user.getpassword()); clIEnt.post(url, params, new AsynchttpResponseHandler() { @OverrIDe public voID onSuccess(int i, header[] headers, byte[] bytes) { if(i == 200) { try { String result = new String(bytes,"utf-8"); Log.e(TAG,"返回结果"+result); try { JsONObject JsonObject=new JsONObject(result); int s=JsonObject.getInt("result"); switch (s){ case 0: Toast.makeText(getApplicationContext(),"该用户不存在",Toast.LENGTH_LONG).show(); break; case 1: Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_LONG).show(); Intent intent=new Intent(LoginActivity.this,ReadActivity.class); startActivity(intent); break; case 2: Toast.makeText(getApplicationContext(),"密码错误",Toast.LENGTH_LONG).show(); break; default: Toast.makeText(getApplicationContext(),"未知错误",Toast.LENGTH_LONG).show(); break; } } catch (JsONException e) { e.printstacktrace(); } } catch (UnsupportedEnCodingException e) { e.printstacktrace(); } } } @OverrIDe public voID onFailure(int i, header[] headers, byte[] bytes, Throwable throwable) { Toast.makeText(getApplicationContext(),"请求失败,请检查网络",Toast.LENGTH_LONG).show(); } }); } private voID initVIEw() { mUsername=(EditText)this.findVIEwByID(R.ID.username); mPassword=(EditText)this.findVIEwByID(R.ID.password); mLogin=(button) this.findVIEwByID(R.ID.login_btn); mGotoregister=(button)this.findVIEwByID(R.ID.goto_register_btn); }}LoginActivity.java
package com.example.fooks;import androID.content.Context;import androID.content.Intent;import androID.os.Bundle;import androID.text.TextUtils;import androID.util.Log;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Toast;import androIDx.appcompat.app.AppCompatActivity;import com.example.fooks.entity.User;import com.loopj.androID.http.AsynchttpClIEnt;import com.loopj.androID.http.AsynchttpResponseHandler;import com.loopj.androID.http.RequestParams;import com.mob.MobSDK;import org.Json.JsONException;import org.Json.JsONObject;import java.io.UnsupportedEnCodingException;import cn.smssdk.EventHandler;import cn.smssdk.SMSSDK;import cz.msebera.androID.httpclIEnt.header;public class Registeractivity extends AppCompatActivity { private button buttonCode,buttonRegister; private EditText editTextPhoneNum,editTextCode; private EditText mR1Password; private EditText mR2Password; private String phoneNum,code; private String pwd1,pwd2; private EventHandler eh; private String TAG="Registeractivity"; public static voID start(Context context) { Intent intent = new Intent(context,Registeractivity.class); context.startActivity(intent); } @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_register); initVIEw(); initListener(); MobSDK.init(this); } public voID initListener() { eh = new EventHandler() { @OverrIDe public voID afterEvent(int event, int result, Object data) { if (result == SMSSDK.RESulT_COMPLETE){ //回调完成 if (event == SMSSDK.EVENT_submit_VERIFICATION_CODE) { //提交验证码成功 runOnUiThread(new Runnable() { @OverrIDe public voID run() { Toast.makeText(Registeractivity.this,"注册成功",Toast.LENGTH_SHORT).show(); } }); }else if (event == SMSSDK.EVENT_GET_VOICE_VERIFICATION_CODE){ runOnUiThread(new Runnable() { @OverrIDe public voID run() { Toast.makeText(Registeractivity.this,"语音验证发送",Toast.LENGTH_SHORT).show(); } }); } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){ //获取验证码成功 runOnUiThread(new Runnable() { @OverrIDe public voID run() { Toast.makeText(Registeractivity.this,"验证码已发送",Toast.LENGTH_SHORT).show(); } }); }else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){ Log.i("test","test"); } }else{ ((Throwable)data).printstacktrace(); Throwable throwable = (Throwable) data; throwable.printstacktrace(); Log.i("1234",throwable.toString()); try { JsONObject obj = new JsONObject(throwable.getMessage()); final String des = obj.optString("detail"); if (!TextUtils.isEmpty(des)){ runOnUiThread(new Runnable() { @OverrIDe public voID run() { Toast.makeText(Registeractivity.this,des,Toast.LENGTH_SHORT).show(); } }); } } catch (JsONException e) { e.printstacktrace(); } } } }; //注册一个事件回调监听,用于处理SMSSDK接口请求的结果 SMSSDK.registerEventHandler(eh); buttonCode.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { phoneNum = editTextPhoneNum.getText().toString().trim(); if(phoneNum.length()==11){ // 获取验证码 SMSSDK.getVerificationCode("86", phoneNum); }else { Toast.makeText(getApplicationContext(),"请输入手机号",Toast.LENGTH_LONG).show(); return; } } }); //点击事件 buttonRegister.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { phoneNum = editTextPhoneNum.getText().toString().trim(); code = editTextCode.getText().toString().trim(); pwd1 =mR1Password.getText().toString().trim(); pwd2 =mR2Password.getText().toString().trim(); if(phoneNum.length()!=11){ Toast.makeText(getApplicationContext(),"请输入正确的手机号",Toast.LENGTH_LONG).show(); }else if(pwd1.equals("")){ Toast.makeText(getApplicationContext(),"请输入密码",Toast.LENGTH_LONG).show(); }else if(pwd2.equals("")){ Toast.makeText(getApplicationContext(),"请输入确认密码",Toast.LENGTH_LONG).show(); }else if(!pwd1.equals(pwd2)){ Toast.makeText(getApplicationContext(),"两次输入密码不一致",Toast.LENGTH_LONG).show(); }else if(code.isEmpty()){ Toast.makeText(getApplicationContext(),"请输入验证码",Toast.LENGTH_LONG).show(); }else{ SMSSDK.submitVerificationCode("86", phoneNum, code); User user =new User(); user.setUsername(phoneNum); user.setPassword(pwd1); Log.e(TAG,"用户名"+user.getUsername()); Log.e(TAG,"密码"+user.getpassword()); Register(user); return; } } }); } protected voID Register(User user) { AsynchttpClIEnt clIEnt =new AsynchttpClIEnt(); String url = "http://47.94.229.72:8080/Fooks/RegisterServlet";//url组成:ip:端口 + 服务端工程名 + servlet名 RequestParams params = new RequestParams(); params.put("username",user.getUsername()); params.put("password",user.getpassword()); clIEnt.post(url, params, new AsynchttpResponseHandler() { @OverrIDe public voID onSuccess(int i, header[] headers, byte[] bytes) { if(i == 200) { try { String result = new String(bytes, "utf-8"); try { JsONObject JsonObject = new JsONObject(result); int s = JsonObject.getInt("result"); switch (s) { case 0: Toast.makeText(getApplicationContext(), "该用户名已被使用", Toast.LENGTH_LONG).show(); break; case 1: Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_LONG).show(); Intent intent = new Intent(Registeractivity.this, ReadActivity.class); startActivity(intent); break; case 2: Toast.makeText(getApplicationContext(), "系统错误", Toast.LENGTH_LONG).show(); break; default: Toast.makeText(getApplicationContext(), "未知错误", Toast.LENGTH_LONG).show(); break; } } catch (JsONException e) { e.printstacktrace(); } Toast.makeText(Registeractivity.this, result, Toast.LENGTH_SHORT).show(); } catch (UnsupportedEnCodingException e) { e.printstacktrace(); } } } @OverrIDe public voID onFailure(int i, header[] headers, byte[] bytes, Throwable throwable) { Toast.makeText(getApplicationContext(),"请求失败,请检查网络",Toast.LENGTH_LONG).show(); } }); } private voID initVIEw() { //初始化控件 buttonCode = findVIEwByID(R.ID.buttonCode); buttonRegister = findVIEwByID(R.ID.buttonRegister); editTextCode = findVIEwByID(R.ID.editTextCode); editTextPhoneNum = findVIEwByID(R.ID.editTextPhoneNum); mR1Password=findVIEwByID(R.ID.r1_password); mR2Password=findVIEwByID(R.ID.r2_password); } // 使用完EventHandler需注销,否则可能出现内存泄漏 @OverrIDe protected voID onDestroy() { super.onDestroy(); SMSSDK.unregisterEventHandler(eh); }}Registeractivity.java
总结
以上是内存溢出为你收集整理的明日计划:团队开发Fooks第八天全部内容,希望文章能够帮你解决明日计划:团队开发Fooks第八天所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)