Android实现简单的注册登录 POST方式提交数据 基于AndroidStudio开发

登录布局页面




    

    

    
    

注册布局页面




    
    

    
    

    

登录页面逻辑

package com.example.latitudeandlongitude.latitudeandlongitude;

        import java.io.InputStream;
        import java.io.OutputStream;
        import java.net.HttpURLConnection;
        import java.net.MalformedURLException;
        import java.net.URL;

        import javax.security.auth.login.LoginException;

        import android.os.Bundle;
        import android.app.Activity;
        import android.content.Intent;
        import android.content.SharedPreferences;
        import android.content.SharedPreferences.Editor;
        import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
        import android.text.TextUtils;
        import android.view.Menu;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.CheckBox;
        import android.widget.EditText;
        import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

    private EditText id;
    private EditText password;
    private Button login;
    private Button register;
    public CheckBox cb;
    private String username;
    private String pwd;
    private SharedPreferences sp;
    private String result;
    private int RequestCode = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        id = (EditText) findViewById(R.id.id);
        password = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);
        //register.setOnClickListener(this);
        login.setOnClickListener(this);
        register = (Button) findViewById(R.id.register);

        register.setOnClickListener(this);
        cb = (CheckBox) findViewById(R.id.check);
        sp = getSharedPreferences("config", 0);
        //将数据显示到UI控件
        //把config.xml文件中的数据取出来显示到EditText控件中
        //如果没找到key键对应的值,会返回第二个默认的值
        String username = sp.getString("username", "");
        String password = sp.getString("password", "");
        id.setText(username);
        this.password.setText(password);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1&&resultCode==2){
            id.setText(data.getStringExtra("id"));
            password.setText(data.getStringExtra("password"));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                username = id.getText().toString().trim();
                pwd = password.getText().toString().trim();
                if(TextUtils.isEmpty(username)||TextUtils.isEmpty(pwd)){
                    Toast.makeText(this, "账号或密码不能为空",Toast.LENGTH_SHORT).show();
                }else{
                    new Thread(){


                        public void run(){
                            try {
                                //设置路径
                                String path="http://192.168.1.111:8080/MyWebsite/androidlogin.do";
                                //创建URL对象
                                URL url=new URL(path);
                                //创建一个HttpURLConnection对象
                                HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                                //设置请求方法
                                conn.setRequestMethod("POST");
                                //设置请求超时时间
                                conn.setReadTimeout(5000);
                                conn.setConnectTimeout(5000);

                                //Post方式不能设置缓存,需要手动设置
                                conn.setUseCaches(false);
                                //设置我们的请求数据
                                String data="id="+username+"&password="+pwd;
                                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//使用的是表单请求类型
                                conn.setRequestProperty("Content-Length", data.length()+"");
                                conn.setDoOutput(true);
                                conn.getOutputStream().write(data.getBytes());
//		                    //连接
//		                    conn.connect();
//							//获取一个输出流
//							OutputStream out=conn.getOutputStream();
//							out.write(data.getBytes());
                                //获取服务器返回的状态吗
                                int code=conn.getResponseCode();
                                if(code==200){
                                    //获取服务器返回的输入流对象
                                    InputStream in= conn.getInputStream();
                                    result = StringTools.readStream(in);
                                    //更新UI
                                    runOnUiThread(new Runnable() {

                                        @Override
                                        public void run() {
                                            if(result.equals("success"))
                                                // TODO Auto-generated method stub
                                                Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
                                            Intent intent = new Intent();
                                            intent.setClassName("com.example.latitudeandlongitude.latitudeandlongitude", "com.example.latitudeandlongitude.latitudeandlongitude.LocationActivity");
                                            startActivity(intent);
                                        }
                                    });
                                }
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        };
                    }.start();
                    if(cb.isChecked()){//判断是否勾选
                        //使用sharePreferences区保存数据 拿到sp实例
                        //参数:  name生成xml文件
                        //mode 模式

                        //获取sp的编辑器
                        Editor editor=sp.edit();
                        //存储用户输入的数据
                        editor.putString("username", username);
                        editor.putString("password", pwd);
                        //提交editor
                        editor.commit();
                    }
                }
                break;
            case R.id.register:
                //跳转到登录页面
                Intent intent=new Intent(this,RegisterActivity.class);
                startActivityForResult(intent,RequestCode);
                break;
            default:
                break;
        }

    }
}

 

注册页面逻辑

package com.example.latitudeandlongitude.latitudeandlongitude;

import android.app.Activity;
import android.view.View;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by zhangwenhiu on 2018/10/28.
 */

public class RegisterActivity extends Activity implements View.OnClickListener{
    private Button register;
    private EditText id;
    private EditText pwd_1;
    private EditText pwd_2;
    private EditText emails;
    private String result;
    private String username;
    private String pwd1;
    private String email;
    private String pwd2;
    private int ResultCode=2;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        register = (Button) findViewById(R.id.register_do);
        register.setOnClickListener(this);
        id = (EditText) findViewById(R.id.id_edit);
        pwd_1 = (EditText) findViewById(R.id.password_edit);
        pwd_2 = (EditText) findViewById(R.id.password_edit_1);
        emails = (EditText) findViewById(R.id.email_edit);
    }

    @Override
    public void onClick(final View v) {
        new Thread(){public void run() {

            switch (v.getId()) {
                case R.id.register_do:
                    username = id.getText().toString().trim();
                    email = emails.getText().toString().trim();
                    pwd1 = pwd_1.getText().toString().trim();
                    pwd2 = pwd_2.getText().toString().trim();

                    //System.out.println("email:"+email);

                    if(!pwd1.equals(pwd2)){
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), "两次输入密码不一致,请重新输入!", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }else {
                        try {
                            //设置路径
                            String path="http://192.168.1.111:8080/MyWebsite/androidregister.do";
                            //?id="+username+"&password=" + pwd1+ "&email=" +email+"
                            //创建URL对象
                            URL url=new URL(path);
                            //创建一个HttpURLconnection对象
                            HttpURLConnection conn =(HttpURLConnection) url.openConnection();
                            //设置请求方法
                            conn.setRequestMethod("POST");
                            //设置请求超时时间
                            conn.setReadTimeout(5000);
                            //conn.setConnectTimeout(5000);
                            //Post方式不能设置缓存,需要手动设置
                            //conn.setUseCaches(false);
                            //准备要发送的数据
                            String data ="id="+URLEncoder.encode(username,"utf-8")+"&password="+URLEncoder.encode(pwd1,"utf-8")+"&email="+URLEncoder.encode(email,"utf-8");
                            //String data ="id="+ username +"&password="+ pwd1 +"&email="+ email+"";
                            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//使用的是表单请求类型
                            conn.setRequestProperty("Content-Length", data.length()+"");
                            conn.setDoInput(true);
                            conn.setDoOutput(true);
                            //连接
                            // conn.connect();
                            //获得返回的状态码
                            conn.getOutputStream().write(data.getBytes());
                            int code=conn.getResponseCode();
                            if(code==200){
                                //获得一个文件的输入流
                                InputStream inputStream= conn.getInputStream();
                                result = StringTools.readStream(inputStream);
                                //更新UI
                                showToast(result);
                            }
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    break;
                default:
                    break;
            }


        };}.start();
    }
    public void showToast(final String content){
        runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                if(result.equals("success")){
                    Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent();
                    intent.putExtra("id", username);
                    intent.putExtra("password", pwd1);
                    setResult(ResultCode, intent);
                    finish();
                }

            }
        });
    }

}

运行截图

Android实现简单的注册登录 POST方式提交数据 基于AndroidStudio开发_第1张图片

 

Android实现简单的注册登录 POST方式提交数据 基于AndroidStudio开发_第2张图片

登录注册demohttps://download.csdn.net/download/xietete/10750331

你可能感兴趣的:(Android开发)