public interface iPresenter { //请求成功后 void success(List<MyBean.ResultBean.NearbyCinemaListBean> list); void error(String error); }
mypresenter
public class MyPresenter implements iPresenter {
//创建view对象
private iView view;
//创建view构造方法
public MyPresenter(iView view) {
this.view = view;
}
//创建modle对象
private MyModle modle = new MyModle();
//请求数据,调用modle获取数据方法
public void getData(String url) {
//放入路径,将本represent对象穿进去,用于回传得到的数据,到成功的方法中
modle.getData(url, this);
}
//继承iPresenter对象,重写获取数据成功的方法
@Override
public void success(List<MyBean.ResultBean.NearbyCinemaListBean> list) {
//成功获取到数据后,将数据传入view对象中
view.success(list);
}
}
public interface iModel { void getData(String url, MyPresenter presenter); }
public class MyModle implements iModel {
//继承iModle接口,重写获取数据方法
@Override
public void getData(String url, final MyPresenter presenter) {
//调用网络封装类OkHttpUtils
MyOkhttpUtils.doGet(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
//成功后解析
Gson gson = new Gson();
MyBean myBean = gson.fromJson(string, MyBean.class);
MyBean.ResultBean result = myBean.getResult();
//解析后回传给presenter.
presenter.success(result.getNearbyCinemaList());
}
});
}
}
public interface iView { void success(List<MyBean.ResultBean.NearbyCinemaListBean> result); }
public class MainActivity extends AppCompatActivity implements iView {
//创建一个空的集合
List<MyBean.ResultBean.NearbyCinemaListBean> list = new ArrayList<>();
//创建一个recycleview
private RecyclerView recyclerView;
//接受消息后执行
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
//将数据对象放在集合中
list = (List<MyBean.ResultBean.NearbyCinemaListBean>) msg.obj;
//创建adapter,将添上数据的集合设置给适配器
MyAdapter myAdapter = new MyAdapter(MainActivity.this, list);
//将recycleview设置上适配器
recyclerView.setAdapter(myAdapter);
}
}
};
private String url = "http://172.17.8.100/movieApi/cinema/v1/findRecommendCinemas?longitude=116.30551391385724&laitude=40.04571807462411&page=1&count=10&userld=18&sessionld=15320748258726";
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycle_view);
image = findViewById(R.id.btn_tiao);
//设置线性
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//创建presenter
MyPresenter myPresenter = new MyPresenter(MainActivity.this);
//调用presenter的获取数据方法
myPresenter.getData(url);
}
//继承iView,重写获取数据成功的方法
@Override
public void success(List<MyBean.ResultBean.NearbyCinemaListBean> result) {
//放在子线程中
Message message = new Message();
message.what = 0;
message.obj = result;
handler.sendMessage(message);
}
}