Android registration function

BaseActivity
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements IBseView {

    public P presenter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);

        setContentView(setLayout());//The returned layout
        initView();
        presenter = getPresenter();//Write it for later judgment, whether it is empty or not
        presenter.attachView(this);
        initData ();
    }

    protected abstract int setLayout();
    protected abstract P getPresenter();
    protected abstract void initData();
    protected abstract void initView();
}
Constant
public class Constant {
    public static final String BASE_URL = "https://www.zhaoapi.cn/";
}

HttpUtilsCallback
public interface HttpUtilsCallback {
    void onSuccess(String str);
    void onFail(int errCode, String errMsg);
}

HttpUtils
public class HttpUtils implements Callback {

    private static HttpUtils INSTANCE;
    private final OkHttpClient okHttpClient;
    private HttpUtilsCallback httpUtilsCallback;

    private HttpUtils(){
        okHttpClient = new OkHttpClient.Builder().build();
    }

    public static HttpUtils getInstence(){
        if (INSTANCE == null){
            INSTANCE = new HttpUtils();
        }
        return INSTANCE;
    }

    public void doPost(String path, HashMap<String,String> map, HttpUtilsCallback httpUtilsCallback){
        this.httpUtilsCallback = httpUtilsCallback;
        FormBody.Builder builder = new FormBody.Builder();
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()){
            String key = iterator.next();
            String value = map.get(key);
            builder.add(key,value);
        }
        FormBody formBody = builder.build();
        Request request = new Request.Builder()
                .url(Constant.BASE_URL+path)
                .post(formBody)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(this);
    }

    @Override
    public void onFailure(Call call, IOException e) {

    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        String string = response.body().string();
        httpUtilsCallback.onSuccess(string);
    }
}
RegisterPresenter
public class RegisterPresenter extends BasePresenter<IMainView> {

    private final HttpUtils httpUtils;

    public RegisterPresenter(){
        httpUtils = HttpUtils.getInstence();
    }

    public void register(String path, HashMap<String, String> map){

        httpUtils.doPost(path, map, new HttpUtilsCallback() {
            @Override
            public void onSuccess(String str) {
                Looper.prepare();//No toast in sub-threads
                IMainView view = getView();
                Gson gson = new Gson ();
                RegisterBean registerBean = gson.fromJson (str, RegisterBean.class);
                String code = registerBean.getCode();
                if (code.equals("1")){
                    Toast.makeText((Context) view,"User is registered",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText((Context) view,"Successful registration",Toast.LENGTH_LONG).show();
                    Intent intent = new Intent((Context) view, LoginActivity.class);
                    ((Context) view).startActivity(intent);
                }
                Looper.loop();
            }

            @Override
            public void onFail(int errCode, String errMsg) {

            }
        });
    }
}

RegisterActivity
public class RegisterActivity extends BaseActivity<RegisterPresenter> implements IMainView, View.OnClickListener {

    private EditText edit_register_phone;
    private EditText edit_register_pass;
    private Button btn_register_sure;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        //setContentView(R.layout.activity_register);

    }

    @Override
    protected int setLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected RegisterPresenter getPresenter() {
        return new RegisterPresenter();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        edit_register_phone = findViewById(R.id.edit_register_phone);
        edit_register_pass = findViewById(R.id.edit_register_pass);
        btn_register_sure = findViewById(R.id.btn_register_sure);
        btn_register_sure.setOnClickListener(this);//Click the registration button
    }

    @Override
    public void onSuccess(String str) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_register_sure:
                String phone = edit_register_phone.getText().toString();
                String pass = edit_register_pass.getText().toString();
                if (phone == null || "".equals(phone) || pass == null){
                    Toast.makeText(RegisterActivity.this,"Mobile phone number and password cannot be empty",Toast.LENGTH_LONG).show();
                    return;
                }
                if (presenter != null){
                    String path = "user/reg";
                    HashMap<String,String> map = new HashMap<>();
                    map.put("mobile",phone);
                    map.put("password",pass);
                    presenter.register(path,map);
                }
                break;
        }
    }
}





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324847044&siteId=291194637