MVP获取网络数据的登陆注册
程序员文章站
2022-04-18 17:57:39
...
CallBack接口
public interface MyCallBack<T> {
void onSuccess(T t);
}
Model层的接口
public interface IModel {
void reQuest(String dataUrl, Class clazz, MyCallBack callback);
}
Persenter层的接口
public interface IPersenter {
void startRequest(String url,Class clazz);
}
View层的接口
public interface IView<T> {
void onSuccess(T data);
}
NetUtils
public class NetUtil {
public static String reQuest(String dataUrl){
String result="";
try {
URL url=new URL(dataUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(3000);
urlConnection.setConnectTimeout(3000);
int responseCode = urlConnection.getResponseCode();
if(responseCode==200){
result=Stream2String(urlConnection.getInputStream());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
private static String Stream2String(InputStream inputStream) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
for (String tmp=bufferedReader.readLine();tmp!=null;tmp=bufferedReader.readLine()){
stringBuilder.append(tmp);
}
return stringBuilder.toString();
}
Model层的实现类
public class IModelImpl implements IModel {
public <T> T reQuest(String dataUrl,Class clazz){
return (T) new Gson().fromJson(NetUtil.reQuest(dataUrl),clazz);
}
@Override
public void reQuest(String dataUrl, final Class clazz, final MyCallBack callback) {
new AsyncTask<String,Void,Object>(){
@Override
protected Object doInBackground(String... strings) {
return reQuest(strings[0],clazz);
}
@Override
protected void onPostExecute(Object o) {
callback.onSuccess(o);
}
}.execute(dataUrl);
}
persenter层的实现类
public class IPersenterImpl implements IPersenter{
private IView mIView;
private IModelImpl model;
public IPersenterImpl(IView iView){
this.mIView=iView;
model=new IModelImpl();
}
public void detach(){
mIView=null;
model=null;
}
@Override
public void startRequest(String url, Class clazz) {
model.reQuest(url, clazz, new MyCallBack() {
@Override
public void onSuccess(Object o) {
mIView.onSuccess(o);
}
});
}
MainActivity
`public class MainActivity extends AppCompatActivity implements` `View.OnClickListener,IView {`
private EditText deng_name,deng_pwd;
private Button button_denglu,button_zhuce;
private String name;
private String pwd;
private IPersenterImpl iPersenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
deng_name=findViewById(R.id.deng_name);
deng_pwd=findViewById(R.id.deng_pwd);
button_denglu=findViewById(R.id.button_denglu);
button_zhuce=findViewById(R.id.button_zhuce);
iPersenter=new IPersenterImpl(this);
button_denglu.setOnClickListener(this);
button_zhuce.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_zhuce:
Intent intent=new Intent(MainActivity.this,ZhuCeActivity.class);
startActivity(intent);
break;
case R.id.button_denglu:
name = deng_name.getText().toString();
pwd = deng_pwd.getText().toString();
iPersenter.startRequest("http://120.27.23.105/user/login?mobile="+name+"&password="+pwd,DengBean.class);
break;
}
}
@Override
public void onSuccess(Object data) {
DengBean dengBean= (DengBean) data;
String code = dengBean.getCode();
if(code.equals("0")){
Toast.makeText(MainActivity.this,"成功",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(MainActivity.this,DengLuActivity.class);
startActivity(intent);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
iPersenter.detach();
}
ZhuActivity
public class ZhuCeActivity extends AppCompatActivity implements View.OnClickListener,IView {
private EditText zhu_name,zhu_pwd;
private Button button_zhu,fan;
private String name;
private String pwd;
private IPersenterImpl iPersenter;
//private String url=;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zhuce);
zhu_name=findViewById(R.id.zhu_name);
zhu_pwd=findViewById(R.id.zhu_pwd);
button_zhu=findViewById(R.id.zhuce);
fan=findViewById(R.id.fan);
iPersenter=new IPersenterImpl(this);
button_zhu.setOnClickListener(this);
fan.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.zhuce:
name = zhu_name.getText().toString();
pwd = zhu_pwd.getText().toString();
iPersenter.startRequest("http://120.27.23.105/user/reg?mobile="+name+"&password="+pwd,ZhuCeBean.class);
break;
case R.id.fan:
Intent intent=new Intent(ZhuCeActivity.this,MainActivity.class);
startActivity(intent);
break;
}
}
@Override
public void onSuccess(Object data) {
ZhuCeBean zhuCeBean= (ZhuCeBean) data;
if(zhuCeBean.getCode().equals("0")){
Toast.makeText(ZhuCeActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(ZhuCeActivity.this,MainActivity.class);
startActivity(intent);
}else{
Toast.makeText(ZhuCeActivity.this,"失败",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
iPersenter.detach();
}
DengActivity
public class DengLuActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_denglu);
button=findViewById(R.id.fanHui);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(DengLuActivity.this,MainActivity.class);
startActivity(intent);
}
});
}