1.权限
1.1banner
implementation 'com.youth.banner:banner:1.4.10'
1.2.okhttp:
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
2.OkHttp
package com.bw.ymy.day12rikao;
import android.os.Handler;
import android.os.Looper;
import com.bw.ymy.day12rikao.callback.MyCallBack;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
public class OkHttpUtils {
private static volatile OkHttpUtils okHttpUtils;
private OkHttpClient mClient;
//handle
private Handler handler=new Handler(Looper.getMainLooper());
//第一步,写一个单例,这里用的懒汉式,也可以用饿汉式
public static OkHttpUtils getIntance()
{
if(okHttpUtils==null)
{
synchronized (OkHttpUtils.class)
{
if(okHttpUtils==null)
{
okHttpUtils=new OkHttpUtils();
}
}
}
return okHttpUtils;
}
//构造方法
private OkHttpUtils()
{
//日志拦截器
HttpLoggingInterceptor interceptor=new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
mClient=new OkHttpClient.Builder()
.addInterceptor(interceptor)
//连接超时
.writeTimeout(10,TimeUnit.SECONDS)
//读取超时
.readTimeout(10,TimeUnit.SECONDS)
.connectTimeout(10,TimeUnit.SECONDS)
.build();
}
//post方法
public void postEnquere(String urlstr, Map<String,String> map, final Class clazz, final MyCallBack myCallBack)
{
//创建表单
FormBody.Builder builder=new FormBody.Builder();
for (Map.Entry<String,String> map1:map.entrySet())
{
builder.add(map1.getKey(),map1.getValue());
}
RequestBody body=builder.build();
Request request=new Request.Builder()
.post(body)
.url(urlstr)
.build();
Call call=mClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
myCallBack.onFail(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string1=response.body().string();
Gson gson=new Gson();
final Object o=gson.fromJson(string1,clazz);
handler.post(new Runnable() {
@Override
public void run() {
myCallBack.onsuccess(o);
}
});
}
});
}
}
3.callback:
package com.bw.ymy.day12rikao.callback;
public interface MyCallBack<T> {
void onsuccess(T data);
void onFail(Exception e);
}
4.M
package com.bw.ymy.day12rikao.model;
import com.bw.ymy.day12rikao.callback.MyCallBack;
import java.util.Map;
public interface IModel {
//post
void getRequest(String urlstr, Map<String,String> map , Class clazz, MyCallBack callBack);
//get
}
继承
package com.bw.ymy.day12rikao.model;
import com.bw.ymy.day12rikao.OkHttpUtils;
import com.bw.ymy.day12rikao.callback.MyCallBack;
import java.util.Map;
public class IModeliml implements IModel {
//post
@Override
public void getRequest(String urlstr, Map<String, String> map, Class clazz, final MyCallBack mycallBack) {
OkHttpUtils.getIntance().postEnquere(urlstr,map,clazz,new MyCallBack()
{
@Override
public void onsuccess(Object data) {
mycallBack.onsuccess(data);
}
@Override
public void onFail(Exception e) {
mycallBack.onFail(e);
}
});
}
}
5.V
package com.bw.ymy.day12rikao.view;
public interface IView<T> {
//请求的返回数据
void onsuccess(T data);
void redata(Exception e);
}
6.P
package com.bw.ymy.day12rikao.presenter;
import java.util.Map;
public interface IPresenter {
void getRequest(String urlstr, Map<String,String> map, Class clazz);
}
继承
package com.bw.ymy.day12rikao.presenter;
import com.bw.ymy.day12rikao.callback.MyCallBack;
import com.bw.ymy.day12rikao.model.IModel;
import com.bw.ymy.day12rikao.model.IModeliml;
import com.bw.ymy.day12rikao.view.IView;
import java.util.Map;
public class IPresenteriml implements IPresenter {
private IView miview;
private IModeliml iModeliml;
public IPresenteriml(IView miview) {
this.miview = miview;
iModeliml=new IModeliml();
}
@Override
public void getRequest(String urlstr, Map<String, String> map, Class clazz) {
iModeliml.getRequest(urlstr, map, clazz, new MyCallBack() {
@Override
public void onsuccess(Object data) {
miview.onsuccess(data);
}
@Override
public void onFail(Exception e) {
miview.redata(e);
}
});
}
public void detach()
{
if(miview!=null)
{
miview=null;
}
if(iModeliml!=null)
{
iModeliml=null;
}
}
}
7.App
package com.bw.ymy.day12rikao;
import android.app.Application;
import android.graphics.Bitmap;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoader.getInstance().init(new ImageLoaderConfiguration.Builder(this)
.memoryCacheSizePercentage(50)
.diskCacheSize(50*1024*1024)
.defaultDisplayImageOptions(new DisplayImageOptions.Builder()
.bitmapConfig(Bitmap.Config.RGB_565)
.cacheOnDisk(true)
.cacheInMemory(true)
.showImageOnLoading(R.mipmap.ic_launcher)
.showImageOnFail(R.mipmap.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.build())
.build());
}
}
8.主页面
package com.bw.ymy.day12rikao;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import com.bw.ymy.day12rikao.Bean.UserBean;
import com.bw.ymy.day12rikao.presenter.IPresenteriml;
import com.bw.ymy.day12rikao.view.IView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;
import com.youth.banner.loader.ImageLoaderInterface;
import java.util.HashMap;
import java.util.Map;
import me.maxwin.view.XListView;
public class MainActivity extends AppCompatActivity implements IView {
private Banner banner;
IPresenteriml iPresenteriml;
private XListView xListView;
private MyBase adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iPresenteriml=new IPresenteriml(this);
banner=findViewById(R.id.banner);
xListView=findViewById(R.id.xlistview);
adapter=new MyBase(this);
xListView.setAdapter(adapter);
banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
banner.setImageLoader(new ImageLoaderInterface<ImageView>() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
UserBean.DataBean userBean= (UserBean.DataBean) path;
//String replace=userBean.getIcon().replace("https","http");
com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(userBean.replace(),imageView);
}
@Override
public ImageView createImageView(Context context) {
ImageView imageView=new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
return imageView;
}
});
Map<String,String> map=new HashMap<>();
map.put("type","0");
iPresenteriml.getRequest("http://www.zhaoapi.cn/ad/getAd",map,UserBean.class);
}
@Override
public void onsuccess(Object data) {
UserBean userBean= (UserBean) data;
banner.setImages(userBean.getData());
banner.start();
adapter.setlist(userBean.getData());
}
@Override
public void redata(Exception e) {
}
}