购物车mvp无ok

《》
package com.bawei.myshouye.net;

import android.os.Environment;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

public class HttpUtils {
private String baseUrl = “http://172.17.8.100”;
//传递头参的构造方法
private Map<String, String> headMap = new HashMap<>();
private HttpListener httpListener;

public HttpUtils setHead(Map<String, String> headMap) {
this.headMap = headMap;
return this;
}

//get请求
public HttpUtils get(String url, Map<String, String> map) {
//当写了请求头map不调用时 在这里判断 new一下否则 会展示不出数据
if (map == null) {
map = new HashMap<>();
}

//缓存sd卡的绝对路径
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
long size = 1024 * 10;
//ok拦截器
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        // request.newBuilder().addHeader() 可以在此添加请求头
        return chain.proceed(request);
    }
}).cache(new Cache(file, size))
        .build();


Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(baseUrl)
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .client(client)
        .build();

HttpService service = retrofit.create(HttpService.class);
service.get(url, headMap, map).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<ResponseBody>() {
            @Override
            public void onSubscribe(Disposable d) {

            }


            @Override
            public void onNext(ResponseBody responseBody) {
                //获取服务端返回的数据
                try {
                    String data = responseBody.string();
                    httpListener.success(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable e) {
                //错误的信息
                String error = e.getMessage();
                httpListener.fail(error);
            }

            @Override
            public void onComplete() {

            }
        });
return this;

}

//通过接口回调将以上数据返回到调用 的类

public void result(HttpListener httpListener) {
this.httpListener = httpListener;
}

public interface HttpListener {
void success(String data);

void fail(String error);

}

}

《model》
public interface Model {
interface CallBackListener {
void success(int type, String data);

void fail(int type, String error);

}

void doget(int type, String url, Map<String, String> map, CallBackListener listener);

1
2
3
4

}

《modeliml》
public class ModelIml implements Model {
@Override
public void doget(final int type, String url, Map<String, String> map, final CallBackListener listener) {
new HttpUtils().get(url, map).result(new HttpUtils.HttpListener() {
@Override
public void success(String data) {
listener.success(type, data);
}

    @Override
    public void fail(String error) {
        listener.fail(type, error);
    }
});

}

1
2
3
4
5
6
7
8

}

作者:偏执青年
来源:优快云
原文:https://blog.youkuaiyun.com/weixin_44666694/article/details/89597315
版权声明:本文为博主原创文章,转载请附上博文链接!

==========

《MainView 》
public interface MainView {

void success(int type, String data);//type 做为区分 是哪个请求

void fail(int type, String error);

}

《MainModel 》
public interface MainModel {

interface OnCallBackListener {
    void success(int type, String data);//type 做为区分 是哪个请求

    void fail(int type, String error);
}

void doShopCar(int type, String url, OnCallBackListener listener);

}

《MainModelIml 》
public class MainModelIml implements MainModel {
private OnCallBackListener listener;
private int type;

@Override
public void doShopCar(int type, String url, OnCallBackListener listener) {
    this.listener = listener;
    this.type = type;
    //请求购物车数据
    OkHttpClient okHttpClient = new OkHttpClient();
    Request request = new Request.Builder().url(url).build();
    final Message message = Message.obtain();
    okHttpClient.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            message.what = 1001;
            message.obj = e.getMessage();
            handler.sendMessage(message);
        }

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

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what == 1000) {//成功
            String data = (String) msg.obj;
            listener.success(type, data);
        } else {
            //失败
            String error = (String) msg.obj;
            listener.success(type, error);
        }
    }
};

}

《MainPresenter 》
public interface MainPresenter {

void doShopCar(int type, String url);

}

《MainPresenterIml 》
public class MainPresenterIml implements MainPresenter, MainModel.OnCallBackListener {
private MainModel mainModel;
private MainView mainView;

public MainPresenterIml(MainModel mainModel, MainView mainView) {
    this.mainModel = mainModel;
    this.mainView = mainView;
}

@Override
public void doShopCar(int type, String url) {
    mainModel.doShopCar(type, url, this);
}

@Override
public void success(int type, String data) {
    mainView.success(type, data);
}

@Override
public void fail(int type, String error) {
    mainView.fail(type, error);
}

//销毁
public void destory() {
    if (mainView != null) {
        mainView = null;
    }
    if (mainModel != null) {
        mainModel = null;
    }
    System.gc();
}

}

内容概要:该论文探讨了一种基于粒子群优化(PSO)的STAR-RIS辅助NOMA无线通信网络优化方法。STAR-RIS作为一种新型可重构智能表面,能同时反射和传输信号,与传统仅能反射的RIS不同。结合NOMA技术,STAR-RIS可以提升覆盖范围、用户容量和频谱效率。针对STAR-RIS元素众多导致获取完整信道状态信息(CSI)开销大的问题,作者提出一种在不依赖完整CSI的情况下,联合优化功率分配、基站波束成形以及STAR-RIS的传输和反射波束成形向量的方法,以最大化总可实现速率并确保每个用户的最低速率要求。仿真结果显示,该方案优于STAR-RIS辅助的OMA系统。 适合人群:具备一定无线通信理论基础、对智能反射面技术和非正交多址接入技术感兴趣的科研人员和工程师。 使用场景及目标:①适用于希望深入了解STAR-RIS与NOMA结合的研究者;②为解决无线通信中频谱资源紧张、提高系统性能提供新的思路和技术手段;③帮助理解PSO算法在无线通信优化问题中的应用。 其他说明:文中提供了详细的Python代码实现,涵盖系统参数设置、信道建模、速率计算、目标函数定义、约束条件设定、主优化函数设计及结果可视化等环节,便于读者理解和复现实验结果。此外,文章还对比了PSO与其他优化算法(如DDPG)的区别,强调了PSO在不需要显式CSI估计方面的优势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值