Control.java
public void login(final Handler handler, final String name, final String pass) { new Thread(new Runnable() { @Override public void run() { String urlString = ACTIONURL + "username=" + name + "&password=" + pass; // 得到网络返回数据 String result = new HTTPService().sendGet(urlString); if (result != null) { // 解析JSON try { JSONObject jsonObject = new JSONObject(result); //返回数据 Message message = new Message(); message.obj = jsonObject.getInt("StateCode"); handler.sendMessage(message); } catch (JSONException e) { e.printStackTrace(); } } } }).start(); }
view层调用方法:
new Control().login(new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg);
//dosomething } }, nameView.getText().toString(), pwdView.getText().toString());
P.S:通过在view中实现handle类并将handle引用传递给Control层,使得View和Control可以进行异步交互.