//MyCallBack 的页面
public interface MyCallBack {
void setSuccess(MyData data);
void setError(String error);
}
//Model页面
void getData(String mUrl,int i, MyCallBack callBack);
//和ModelImpl的页面
public class ModelImpl implements Model{
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 0:
String jsonstr= (String) msg.obj;
Gson gson=new Gson();
MyData data= gson.fromJson(jsonstr,MyData.class);
callBack.setSuccess(data);
break;
}
}
};
private MyCallBack callBack;
@Override
public void getData(final String mUrl,final int i, final MyCallBack callBack) {
this.callBack=callBack;
new Thread(new Runnable() {
@Override
public void run() {
try {
String ok= OkHttps.getintent().get(mUrl,i);
handler.sendMessage(handler.obtainMessage(0,ok));
} catch (IOException e) {
callBack.setError(".......");
}
}
}).start();
}
}
//perener的页面
void startRequest(String mUrl,int i);
//perenerimpl的页面
public class PerenerImpl implements Perener{
private IVew iVew;
private ModelImpl model;
public PerenerImpl(IVew iVew) {
this.iVew = iVew;
model=new ModelImpl();
}
@Override
public void startRequest(String mUrl,int i) {
model.getData(mUrl, i,new MyCallBack() {
@Override
public void setSuccess(MyData data) {
iVew.setSuccess(data);
}
@Override
public void setError(String error) {
iVew.setError(error);
}
});
}
}
//IVew 的页面
public interface IVew {
void setSuccess(MyData data);
void setError(String error);
}
//OkHttps的页面
public class OkHttps {
private OkHttpClient okHttpClient;
private OkHttps() {
okHttpClient=new OkHttpClient();
}
public static OkHttps getintent() {
return OkHolder.https;
}
static class OkHolder{
private static final OkHttps https = new OkHttps();
}
//同步get
public String get(String mUrl,int i) throws IOException {
Request request = new Request.Builder().url(mUrl+i).build();
Response execute = okHttpClient.newCall(request).execute();
return execute.body().string();
}
}