明日计划:团队开发Fooks第八天

一、明日计划

 

完成书籍上传与信息保存的数据库交互,开始设计书籍下载

二、今日成果

 

 

 

 

 

 

 

 成功实现登录注册的整合,完成了在云端数据库的存储

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

 

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...