登录,注册,URLConnection

登录Activity:
public class MainActivity extends BaseActivity implements ContarctInterface.View {

    public EditText edit_phone,edit_password;
    public TextView text_register;
    public Button button_login;
    public LoginPresenter loginPresenter;
    public String aphone,password;
    public SharedPreferences sp;
    public CheckBox checkBox;

    @Override
    protected void initView() {

        edit_phone=findViewById(R.id.editText_phone);
        edit_password=findViewById(R.id.editText_password);
        text_register=findViewById(R.id.textview_register);
        checkBox=findViewById(R.id.checkBox);
        button_login=findViewById(R.id.button_login);

        loginPresenter=new LoginPresenter();
        loginPresenter.attch(this);


        button_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                aphone=edit_phone.getText().toString();
                password=edit_password.getText().toString();

                if (!aphone.isEmpty()&&!password.isEmpty()) {
                    loginPresenter.getModel(Api.login,aphone,password);
                }else {
                    Toast.makeText(MainActivity.this, "请输入手机号和密码", Toast.LENGTH_SHORT).show();
                }
            }
        });

        text_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,RegistActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void getPresenter(String data) {

        Gson gson=new Gson();
        Login login=gson.fromJson(data,Login.class);
        if (login.getStatus().equals("0000")) {
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(MainActivity.this,SelecterActivity.class);
            intent.putExtra("phone",aphone);
            startActivity(intent);
            finish();
        }else {
            Toast.makeText(this, "请先注册", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loginPresenter.detch();
    }
}

注册Activity:
public class RegistActivity extends BaseActivity implements ContarctInterface.View {

    public EditText edit_phone,edit_password,editText_confirm_password;
    public TextView text_login;
    public Button button_register;
    public RegistPresenter registPresenter;
    public String phone,password,confirm_password;

    @Override
    protected void initView() {

        edit_phone=findViewById(R.id.edit_phone);
        edit_password=findViewById(R.id.edit_password);
        editText_confirm_password=findViewById(R.id.edit_confirm_password);
        text_login=findViewById(R.id.text_login);
        button_register=findViewById(R.id.button_register);

        registPresenter=new RegistPresenter();
        registPresenter.attch(this);

        button_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                phone=edit_phone.getText().toString();
                password=edit_password.getText().toString();
                confirm_password=editText_confirm_password.getText().toString();

                if (!phone.isEmpty()&&!password.isEmpty()&&phone.length()>10) {
                    registPresenter.getModel(Api.regist,phone,password);
                }else if (password == confirm_password) {
                    Toast.makeText(RegistActivity.this, "请检验手机号和密码", Toast.LENGTH_SHORT).show();
                }
            }
        });
        text_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(RegistActivity.this,MainActivity.class));
            }
        });
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_regist;
    }

    @Override
    public void getPresenter(String data) {

        Gson gson=new Gson();
        Regist regist=gson.fromJson(data,Regist.class);
        if (regist.equals("1001")) {
            Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
        }
        startActivity(new Intent(RegistActivity.this,MainActivity.class));
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        registPresenter.detch();
    }
}

工具类:

public class URLConnection {

    private static URLConnection ourInstance=new URLConnection();

    public static boolean isconn(Context context){

        ConnectivityManager manager= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();

        if (info!=null){
            Toast.makeText(context,"有网络",Toast.LENGTH_SHORT).show();
            return true;
        }else{
            Toast.makeText(context,"没有网络",Toast.LENGTH_SHORT).show();
            return false;
        }
    }

    public static URLConnection getInstance() {

        if (ourInstance == null) {

            ourInstance=new URLConnection();
        }
        return ourInstance;
    }
    private URLConnection(){

    }

    public void PostData(String url,String name,String password,final CallBack callBack){

        new AsyncTask<String,Void,String>(){

            @Override
            protected String doInBackground(String... strings) {
                return postasynctask(strings[0],strings[1],strings[2]);
            }
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                callBack.onSuccess(s);
            }
        }.execute(url,name,password);
    }

    private String postasynctask(String url, String name, String password) {

        try {
            URL url1 = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) url1.openConnection();

            connection.setRequestMethod("POST");
            connection.setReadTimeout(5000);
            connection.setConnectTimeout(5000);

            //请求头
            String body = "phone="+URLEncoder.encode(name)+"&pwd="+URLEncoder.encode(password);
            connection.getOutputStream().write(body.getBytes());
            if (connection.getResponseCode()==200){
                InputStream inputStream = connection.getInputStream();
                String s = new String(ByteStreams.toByteArray(inputStream));
                return s;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String GetData(String murl, CallBack callBack) {
        try {
            URL url=new URL(murl);
            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
            int code= connection.getResponseCode();
            if (code == 200) {
                InputStream inputstream=connection.getInputStream();
                BufferedReader reader=new BufferedReader(new InputStreamReader(inputstream));
                StringBuilder builder=new StringBuilder();
                String aaa="";
                while ((aaa=reader.readLine())!=null){

                    builder.append(aaa);
                }
                return builder.toString();

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    //定义接口
    public interface CallBack{
        public void onSuccess(String result);
        public void onError();
    }
}
 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值