一个重要知识就是通过接口回调实现数据的传递

结构:

MVP_Text

view模型c

在视图这里可能会报这个错误​​Only the original thread that created a view hierarchy can touch its views.​​​,
此时你需要handle来更新视图,你有其他方法也行

public class MVPActivity extends AppCompatActivity implements ViewInterface {

private Button button;
private EditText editText;
private TextView textView;
private MvpPresenter presenter;
private String data;

private Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 2:
{
textView.setText(data);
}break;
default:
break;
}
}
};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mvp);
initView();
setLisenter();
presenter = new MvpPresenter(this);
}

private void initView() {
button = findViewById(R.id.bt_mvp);
editText = findViewById(R.id.et_mvp);
textView = findViewById(R.id.tv_mvp);
}

private void setLisenter() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String city = String.valueOf(editText.getText());
if (TextUtils.isEmpty(city)){
city="北京";
}

presenter.getWeather(city);
}
});
}

@Override
public void updateWeather(String resData) {
data = resData;
Message message = new Message();
message.what=2;
handler.sendMessage(message);
}
}

重要的接口实现
视图接口回调,需要activity实现

public interface ViewInterface {
void updateWeather(String data);
}

Presenter模型

**需要一个​​视图接口​​​和一个​​模型接口​​,在这里模型是使用了静态方法调用,差不多

public class MvpPresenter {
private static final String TAG="AAA";

private ViewInterface viewInterface;

public MvpPresenter(ViewInterface viewInterface) {
this.viewInterface = viewInterface;
}

public void getWeather(String city){

Log.d(TAG, "getWeather: MvpPresenter"+city);
Model.getWeather(city, new ModelCallback() {

@Override
public void onSuccess(String resData) {
viewInterface.updateWeather(resData);
Log.d(TAG, "onSuccess: "+resData);
}

@Override
public void onFailure(String errorData) {
Log.d(TAG, "onFailure: ");
}

@Override
public void onComplete() {
Log.d(TAG, "onComplete: ");
}
});
}

}

model模型

/**
* callback是反馈数据给presenter层
*/
public interface ModelCallback {
/**
*
* @param resData 接受到的数据
*/
void onSuccess(String resData);

/**
*
* @param errorData 返回给上一层的错误信息
*/
void onFailure(String errorData);

/**
*
*/
void onComplete();
}
public class Model {
private static final String TAG="AAA";
private static String data;

public static void getWeather(final String city,final ModelCallback callback){
requestWeather(city, callback);
}

private static void requestWeather(String city, final ModelCallback callback) {
OkHttpClient client = new OkHttpClient();
final Request request = new Request.Builder()
.get()
.url(Comment.BASH_URL+"?city="+city+"&key="+Comment.KEY)
.build();

client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.d(TAG, "onFailure: ");
callback.onFailure("错误");
}

@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
data = response.body().string();
Log.d(TAG, "onResponse: "+data);
callback.onSuccess(data);
}
}
});
}


}