必威体育Betway必威体育官网
当前位置:首页 > IT技术

w1

时间:2019-09-20 08:41:06来源:IT技术作者:seo实验室小编阅读:86次「手机版」
 

w1

---------------------------------------------------------httputils------------------------------------------------------

private Httplistner listner;

private int HTTP_SUCCESS = 1000;

private int HTTP_Bai = 1001;

private handler handler = new Handler(){

@Override

public void handlemessage(Message msg) {

super.handleMessage(msg);

if (msg.what==HTTP_SUCCESS){

String data = (String) msg.obj;

listner.success(data);

}else {

listner.bai();

}

}

};

public HttpUtils get(String url){

doHttp(url,0,null);

return this;

}

public HttpUtils post(String url,FormBody.builder bodybuid){

doHttp(url,1,bodybuid);

return this;

}

private void doHttp(String url, int type, FormBody.Builder bodybuid) {

Okhttpclient client = new okhttpClient.Builder().addInterceptor(new lanjie()).build();

request.Builder builder = new Request.Builder();

builder.url(url);

if (type==0){

builder.get();

}else {

Requestbody body = bodybuid.build();

builder.post(body);

}

final Message message = Message.obtain();

Request request = builder.build();

client.newCall(request).enqueue(new Callback() {

@Override

public void onFailure(Call call, IOException e) {

message.what=HTTP_BAI;

handler.sendMessage(message);

}

    @Override
    public void onresponse(Call call, Response response) throws IOException {
        message.obj = response.body().string();
        message.what = HTTP_SUCCESS;
        handler.sendMessage(message);
    }
});

}

public void result(Httplistner listner){

this.listner = listner;

}

public interface Httplistner{

void success(String data);

void bai();

}

public class lanjie implements Interceptor {

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

    return chain.proceed(request);
}

}

---------------------------------------------------------mvp------------------------------------------------------

---------------------------------------------------------models------------------------------------------------------

interface CallBacko{

void success(String data);

void bai();

}

void getData(CallBacko callBacko);

---------------------------------------------------------model------------------------------------------------------

@Override

public void getData(final CallBacko callBacko) {

new HttpUtils().get(“http://gank.io/api/data/福利/10/1”).result(new HttpUtils.Httplistner() {

@Override

public void success(String data) {

callBacko.success(data);

}

    @Override
    public void bai() {
        callBacko.bai();
    }
});
---------------------------------------------------------views------------------------------------------------------
void success(String data);

void bai();

---------------------------------------------------------presenters------------------------------------------------------

void doData();

---------------------------------------------------------presenter------------------------------------------------------

private Model model;

private Views views;

public Presenter(Model model, Views views) {

this.model = model;

this.views = views;

}

@Override

public void doData() {

model.getData(this);

}

@Override

public void success(String data) {

views.success(data);

}

@Override

public void bai() {

views.bai();

}

public void destroy(){

if (model!=null){

model=null;

}

if (views!=null){

views=null;

}

}

-----------------------------------------------------适配器---------------------------------------------------------------

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {

private ArrayList list = new ArrayList<>();

private context context;

public NewsAdapter(Context context) {
    this.context = context;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = View.inflate(context, R.layout.item, null);
    ViewHolder viewHolderl = new ViewHolder(view);
    return viewHolderl;

}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
    String type = list.get(i).getType();
    String url = list.get(i).getUrl();
    viewHolder.text.setText(type);
    picasso.with(context).load(url).into(viewHolder.image);

}

@Override
public int getItemCount() {
    return list.size();
}
public void setData(ArrayList<One> list){
    this.list = list;
    notifydatasetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {

    private final ImageView image;
    private final TextView text;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        image = itemView.findViewById(R.id.image);
        text = itemView.findViewById(R.id.text);
    }
}

}

--------------------------------------------mainactivity--------------------------------------------------------------

private XRecyclerView xre;

private Presenter presenter;

private NewsAdapter adapter;

private ArrayList list = new ArrayList<>();

@Override

protected void onCreate(Bundle savedinstanceState) {

super.onCreate(savedInstanceState);

setcontentView(R.layout.activity_main);

xre = findViewById(R.id.xre);

LinearLayoutManager layoutManager = new LinearLayoutManager(this);

layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

xre.setLayoutManager(layoutManager);

adapter = new NewsAdapter(this);

xre.setAdapter(adapter);

xre.setLoadingMoreEnabled(true);
xre.setLoadingListener(new XRecyclerView.LoadingListener() {
    @Override
    public void onRefresh() {
        presenter.doData();
    }

    @Override
    public void onLoadMore() {
        presenter.doData();
    }
});
presenter=new Presenter(new Model(),this);
presenter.doData();

}

@Override

public void success(String data) {

JsonBean jsonBean = new Gson().fromJson(data, JsonBean.class);

list = jsonBean.getResults();

adapter.setData(list);

xre.loadMoreComplete();

xre.refreshComplete();

}

@Override

public void bai() {

xre.loadMoreComplete();

xre.refreshComplete();

}

@Override

protected void onDestroy() {

super.onDestroy();

presenter.destroy();

}

相关阅读

DW15.exe是什么进程文件

DW15.exe是什么文件? DW15.exe是微软的IE错误报告程序。 在C:\Program Files\Internet Explorer下的iedw.exe是安全的。这个的

window10下架设VPN服务器

我们直入主题,看看架设VPN服务器的具体步骤: 1.点击右下角的网络图标,然后选择“打开网络和共享中心”,再选择“更改适配器设置” 2.

mercury无线路由器设置图文教程(水星MW150R)

下面我们就介绍一下mercury无线路由器设置,文中以MW150R无线路由器为示例。如果路由器之前被设置过的请复位路由器后按教程操作。

分享到:

栏目导航

推荐阅读

热门阅读