okhttp
package com.example.login.moder;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import com.example.login.presenter.MainPresenter;
import com.google.gson.Gson;
import java.io.IOException;
import java.net.URL;
import java.sql.Time;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class Okhttp<T> {
private static Okhttp okhttp;
private final OkHttpClient okHttpClient;
private CallBack callBack;
public Okhttp(){
okHttpClient = new OkHttpClient.Builder()
.readTimeout(5,TimeUnit.SECONDS)
.connectTimeout(5,TimeUnit.SECONDS)
.build();
}
public static Okhttp getinokhttp(){
return getokhttp.okhttp;
}
private static class getokhttp{
public static Okhttp okhttp=new Okhttp();
}
public void postData(String url, HashMap<String, String> map, final Class<T> beanClass, CallBack<Bean> callBack){
this.callBack=callBack;
FormBody.Builder formbody = new FormBody.Builder();
if(formbody!=null){
for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
formbody.add(stringStringEntry.getKey(),stringStringEntry.getValue());
}
}
Request builder = new Request.Builder()
.url(url)
.post(formbody.build())
.build();
okHttpClient.newCall(builder).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Okhttp.this.callBack.onfailure("错误");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Log.e("my",""+string);
Gson gson=new Gson();
T t = gson.fromJson(string,beanClass);
Message message = handler.obtainMessage();
message.obj=t;
handler.sendMessage(message);
}
});
}
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
T t=(T)msg.obj;
callBack.onresponse(t);
}
};
public interface CallBack<D>{
void onresponse(D d);
void onfailure(String msg);
}
}
basepresenter
package com.example.login.presenter;
import com.example.login.view.interfaces.IBaseView;
public class BasePresenter<T> implements IBaseView{
private T mview;
public T getMview() {
return mview;
}
public void setMview(T mview) {
this.mview = mview;
}
public void datch(){
this.mview=null;
}
}
mainpresenter
package com.example.login.presenter;
import android.util.Log;
import com.example.login.moder.Bean;
import com.example.login.moder.Okhttp;
import com.example.login.view.interfaces.IMainView;
import java.util.HashMap;
public class MainPresenter extends BasePresenter<IMainView<Bean>> {
public void postData(String url, HashMap<String,String> map) {
Okhttp.getinokhttp().postData(url,map,Bean.class, new Okhttp.CallBack<Bean>() {
@Override
public void onresponse(Bean bean) {
Log.e("mys",""+bean);
if(bean!=null){
getMview().onSeccuss(bean);
}
}
@Override
public void onfailure(String msg) {
getMview().onErr(msg);
}
});
}
}
baseActivity
package com.example.login.view.activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.example.login.R;
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(Layou());
initView();
initData();
}
protected abstract int Layou();
protected abstract void initData();
protected abstract void initView();
}
登录Activity
package com.example.login.view.activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.login.R;
import com.example.login.moder.Bean;
import com.example.login.presenter.MainPresenter;
import com.example.login.view.interfaces.IMainView;
import java.util.HashMap;
public class MainActivity extends BaseActivity implements IMainView<Bean> {
private MainPresenter mainPresenter;
private TextView textView;
private HashMap<String,String> map;
private EditText pwd;
private EditText phone;
private String path="http://172.17.8.100/small/user/v1/login";
private Button button;
private String message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected int Layou() {
return R.layout.activity_main;
}
@Override
protected void initData() {
mainPresenter = new MainPresenter();
mainPresenter.setMview(this);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,ScendActivity.class);
startActivity(intent);
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
map=new HashMap<>();
map.put("phone",phone.getText().toString());
map.put("pwd",pwd.getText().toString());
mainPresenter.postData(path,map);
}
});
}
@Override
protected void initView() {
textView = findViewById(R.id.zc);
pwd = findViewById(R.id.pwd);
phone = findViewById(R.id.phone);
button = findViewById(R.id.button);
}
@Override
public void onSeccuss(Bean o) {
message=o.message;
Toast.makeText(MainActivity.this,message,Toast.LENGTH_SHORT).show();
if(message=="登录成功"){
Toast.makeText(MainActivity.this,"登录成功666",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onErr(String s) {
}
}
注册页面
package com.example.login.view.activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.login.R;
import com.example.login.moder.Bean;
import com.example.login.presenter.MainPresenter;
import com.example.login.view.interfaces.IMainView;
import java.util.HashMap;
public class ScendActivity extends BaseActivity implements IMainView<Bean> {
private EditText phone;
private EditText pwd;
private Button button;
private HashMap<String,String> map;
private MainPresenter mainPresenter;
private String message;
private String url="http://172.17.8.100/small/user/v1/register";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected int Layou() {
return R.layout.activity_main2;
}
@Override
protected void initData() {
mainPresenter = new MainPresenter();
mainPresenter.setMview(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
map =new HashMap<>();
map.put("pwd",pwd.getText().toString());
map.put("phone",phone.getText().toString());
mainPresenter.postData(url,map);
}
});
}
@Override
protected void initView() {
phone = findViewById(R.id.phone);
pwd = findViewById(R.id.pwd);
button = findViewById(R.id.zc);
}
@Override
public void onSeccuss(Bean o) {
message=o.message;
Toast.makeText(this,message,Toast.LENGTH_LONG).show();
}
@Override
public void onErr(String s) {
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.activity.MainActivity">
<RelativeLayout
android:id="@+id/re"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:hint="手机号"
android:id="@+id/phone"
android:layout_marginTop="300dp"
android:layout_width="200dp"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
/>
<EditText
android:hint="登录密码"
android:layout_centerHorizontal="true"
android:layout_below="@id/phone"
android:id="@+id/pwd"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
<CheckBox
android:layout_below="@id/pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/pwd"
android:layout_marginRight="-80dp"
android:text="记住密码"/>
<TextView
android:layout_below="@id/pwd"
android:id="@+id/zc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="-50dp"
android:layout_toRightOf="@id/pwd"
android:text="快速注册" />
</RelativeLayout>
<Button
android:id="@+id/button"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="@+id/re"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:text="登录" />
</RelativeLayout>