RecyclerView和XRecyclerView的布局和实现的MVP

//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();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值