LoginActivity是我在MainActivity做的跳转这里不用管
mplementation 'com.squareup.okhttp3:okhttp:3.6.0'
implementation 'com.squareup.okio:okio:1.11.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.github.bumptech.glide:glide:4.9.0'
public interface IView {
//获取数据显示
public void getView(Object data);
}
public interface Ipresenter {
//接收对象
void getPreData();
//解绑对象
void attchView(IView iView);
void Detchview();
}
public interface Model {
public void getDate(MCallBack mCallBack);
interface MCallBack{
//成功
public void onSuccess(Object data);
//失败
public void onFail(Object error);
}
}
public class MainActivity extends AppCompatActivity implements IView{
private LoginPresenterImpI presenterImpI;
private RecyclerView recy_view;
private jsonBean jsonBean;
private ShopAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
presenterImpI = new LoginPresenterImpI();
presenterImpI.attchView(this);
presenterImpI.getPreData();
initView();
}
private void initView(){
recy_view = findViewById(R.id.recy_view);
recy_view.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public void getView(Object data) {
if (data!=null){
jsonBean=(com.bawei.moni.bean.jsonBean) data;
adapter = new ShopAdapter(MainActivity.this, jsonBean);
recy_view.setAdapter(adapter);
adapter.setRecyclerviewListener(new ShopAdapter.RecyclerviewListenner(){
@Override
public void callBack(int id) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
intent.putExtra("id",id);
startActivity(intent);
}
});
}
}
}
public class ShopAdapter extends RecyclerView.Adapter {
Context context;
jsonBean jsonbean;
public ShopAdapter(Context context, jsonBean jsonbean) {
this.context = context;
this.jsonbean = jsonbean;
}
@NonNull
@Override
public ShopAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item,viewGroup,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ShopAdapter.ViewHolder viewHolder, int i) {
Glide.with(context).load(jsonbean.getResult().getPzsh().getCommodityList()
.get(i).getMasterPic()).into(viewHolder.iamge);
viewHolder.text.setText(jsonbean.getResult().getPzsh().getCommodityList().get(i).getCommodityName());
viewHolder.itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
recyclerviewListenner.callBack(v.getId());
}
});
}
@Override
public int getItemCount() {
return jsonbean.getResult().getPzsh().getCommodityList().size();
}
//初始化视图
public class ViewHolder extends RecyclerView.ViewHolder{
private final TextView text;
private final ImageView iamge;
public ViewHolder(@NonNull View itemView) {
super(itemView);
iamge = itemView.findViewById(R.id.image_view);
text = itemView.findViewById(R.id.text_view);
}
}
RecyclerviewListenner recyclerviewListenner;
public void setRecyclerviewListener(RecyclerviewListenner recyclerviewListenner) {
this.recyclerviewListenner = recyclerviewListenner;
}
public interface RecyclerviewListenner{
void callBack(int id);
}
}
public class LoginPresenterImpI implements Ipresenter{
//m层的接口
private Model model;
MainActivity mainActivity;
private SoftReference modelSoftReference;
@Override
public void getPreData() {
model.getDate(new Model.MCallBack(){
@Override
public void onSuccess(Object data) {
mainActivity.getView(data);
}
@Override
public void onFail(Object error) {
}
});
}
@Override
public void attchView(IView iView) {
this.mainActivity = (MainActivity) iView;
//m层的类
model = new loginModelImpI();
modelSoftReference = new SoftReference(model);
}
@Override
public void Detchview () {
}
}
public class loginModelImpI implements Model {
//请求的接口
String Base_url="http://172.17.8.100/small/commodity/v1/commodityList";
@Override
public void getDate(final MCallBack mCallBack) {
OkHttp.getInstance().doGet(Base_url, jsonBean.class,new OkHttp.NetCallBack(){
@Override
public void LoadSuccess(Object obj) {
mCallBack.onSuccess(obj);
}
@Override
public void LoadFail() {
}
});
}
}
public class OkHttp {
private OkHttpClient client;
private static volatile OkHttp instance;
private Handler handler = new Handler();
//创建拦截器
private Interceptor getAppInterceptor()
{
Interceptor interceptor= new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Log.e("++++++","拦截前");
Response response = chain.proceed(request);
Log.e("++++++","拦截后");
return response;
}
};
return interceptor;
}
//添加拦截器
private OkHttp()
{
File file = new File(Environment.getExternalStorageDirectory(), "cache1");
client = new OkHttpClient().newBuilder()
.readTimeout(3000,TimeUnit.SECONDS)
.connectTimeout(3000,TimeUnit.SECONDS)
.addInterceptor(getAppInterceptor())
.cache(new Cache(file,10*1024))
.build();
}
//创建单例
public static OkHttp getInstance()
{
if(instance == null)
{
synchronized (OkHttp.class)
{
if(null == instance)
{
instance = new OkHttp();
}
}
}
return instance;
}
//封装doget方法
public void doGet(String url, final Class clazz, final NetCallBack netCallBack)
{
// 2. 创建一个请求对象
Request request = new Request.Builder()
.get()
.url(url)
.build();
//创建call对象
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Gson gson = new Gson();
final Object o = gson.fromJson(string, clazz);
handler.post(new Runnable() {
@Override
public void run() {
netCallBack.LoadSuccess(o);
}
});
}
});
}
//封装dopost请求
public void doPost(String url, final Class clazz, String name,String pwd, final NetCallBack netCallBack)
{
//新建okhttp对象
client = new OkHttpClient();
/**
* 通过体传值
*/
FormBody build = new FormBody.Builder()
.add("phone", name)
.add("pwd", pwd)
.build();
Request request = new Request.Builder()
.url(url)
.post(build)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
Gson gson = new Gson();
final Object o = gson.fromJson(string, clazz);
handler.post(new Runnable() {
@Override
public void run() {
netCallBack.LoadSuccess(o);
}
});
}
});
}
public interface NetCallBack{
void LoadSuccess(Object obj);
void LoadFail();
}
}