Banner和OkHttp的结合使用

来源:互联网 发布:矢量数据的概念 编辑:程序博客网 时间:2024/06/05 19:00

   Banner和OkHttp的结合使用

先导入依赖或架包,可以去网上下载

 compile files('libs/gson-2.2.4.jar')    compile 'com.youth.banner:banner:1.4.9'    compile files('libs/glide-3.7.0.jar')    compile files('libs/okhttp-3.9.0.jar')    compile files('libs/okio-1.13.0.jar')
再添加网络权限
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

activity_main布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent">    <com.youth.banner.Banner        android:id="@+id/ban"        android:layout_width="match_parent"        android:layout_height="match_parent"></com.youth.banner.Banner></RelativeLayout>
MainActivity

 import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import com.google.gson.Gson;import com.youth.banner.Banner;import java.io.IOException;import java.util.ArrayList;import java.util.List;import okhttp3.Call;public class MainActivity extends AppCompatActivity {    private Banner mBan;   List<String> list=new ArrayList<>();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mBan = (Banner) findViewById(R.id.ban);        OkUrl();    }    private void OkUrl() {        OkHttpUtils.getInstance().doGet("http://news-at.zhihu.com/api/4/news/latest", new OnUiCallback() {            @Override            public void onFailed(Call call, IOException e) {            }            @Override            public void onSuccess(String result) throws IOException {                Gson gson=new Gson();                Beans beans = gson.fromJson(result, Beans.class);                for (int i=0;i<beans.getTop_stories().size();i++){                   list.add(beans.getTop_stories().get(i).getImage());                }             mBan.setImageLoader(new Img());                mBan.setImages(list);                mBan.start();            }        });    }}

ImageLoader

 import android.content.Context;import android.widget.ImageView;import com.bumptech.glide.Glide;import com.youth.banner.loader.ImageLoader;public class Img extends ImageLoader {    @Override    public void displayImage(Context context, Object path, ImageView imageView) {        Glide.with(context)                .load(path)                .into(imageView);    }}
OkHttp

 import android.os.Handler;import okhttp3.Call;import okhttp3.Callback;import okhttp3.OkHttpClient;import okhttp3.Request;public class OkHttpUtils {    private Handler handler=new Handler();    public Handler getHandler(){        return handler;    }    private static OkHttpUtils okHttpUtils=new OkHttpUtils();    private OkHttpUtils(){};    public static OkHttpUtils getInstance(){        return okHttpUtils;    }    private OkHttpClient client;    private void initOkHttpClient(){        if(client==null){            client=new OkHttpClient().newBuilder().build();        }    }    public void doGet(String url, Callback callback){        initOkHttpClient();        Request request=new Request.Builder().url(url).build();        Call call=client.newCall(request);        call.enqueue(callback);    }}
OnUiCallback
 import android.os.Handler;import java.io.IOException;import okhttp3.Call;import okhttp3.Callback;import okhttp3.Response;public abstract class OnUiCallback implements Callback {    private Handler handler=OkHttpUtils.getInstance().getHandler();    public abstract void onFailed(Call call,IOException e);    public abstract void onSuccess(String result)throws IOException;    @Override    public void onFailure(final Call call, final IOException e) {        handler.post(new Runnable() {            @Override            public void run() {                onFailure(call,e);            }        });    }    @Override    public void onResponse(Call call, final Response response) throws IOException {        final String result=response.body().string();        handler.post(new Runnable() {            @Override            public void run() {                try {                    onSuccess(result);                } catch (IOException e) {                    e.printStackTrace();                }            }        });    }}
Bean类

 import java.util.List;public class Beans {    /**     * date : 20171030     * stories : [{"images":["https://pic4.zhimg.com/v2-b7f285f4d34102518689f648c6d1f303.jpg"],"type":0,"id":9654447,"ga_prefix":"103015","title":"电动车取代内燃机,除了技术之外更重要的是国家安全"},{"images":["https://pic3.zhimg.com/v2-b1a8a3462e8671363f2c89a351eb07ea.jpg"],"type":0,"id":9654435,"ga_prefix":"103014","title":"零安全剂量的毒物,怎么就不能谈剂量了?"},{"images":["https://pic4.zhimg.com/v2-389d9dac7ef8c63dc55138e07a4f6f87.jpg"],"type":0,"id":9654384,"ga_prefix":"103013","title":"夫妻本是同林鸟,大难临头\u2026\u2026先等会再飞"},{"images":["https://pic2.zhimg.com/v2-2a9422904dab5de7a89dc172f1881111.jpg"],"type":0,"id":9654352,"ga_prefix":"103012","title":"大误 · 谁教你乱用红烧肉的?"},{"images":["https://pic3.zhimg.com/v2-93c20680fbc14392f3b60616f44f15c2.jpg"],"type":0,"id":9654269,"ga_prefix":"103011","title":"关于「吃」这件事,大脑说:太有意思了\u2026\u2026"},{"images":["https://pic3.zhimg.com/v2-546645d11b8fa37f213f8c8a3d7addde.jpg"],"type":0,"id":9654314,"ga_prefix":"103010","title":"- 参加这种学术会议,巨无聊吧\r\n- 那是你打开方式不对"},{"images":["https://pic1.zhimg.com/v2-8f821d4ced55d064a7e05f95b18171e0.jpg"],"type":0,"id":9653819,"ga_prefix":"103009","title":"玩了这么多游戏,你想过中国游戏市场的未来会是什么样吗?"},{"images":["https://pic3.zhimg.com/v2-65f3f69135531bc5d8e846d9137e9016.jpg"],"type":0,"id":9654362,"ga_prefix":"103008","title":"这群怪咖,用最土的方式拍出豆瓣评分最高的网剧"},{"images":["https://pic4.zhimg.com/v2-b0776464f85dd38e70a8f91c355d7cdf.jpg"],"type":0,"id":9654369,"ga_prefix":"103007","title":"在美国加入「兄弟会」:勇气可嘉,但还是要擦亮双眼"},{"images":["https://pic3.zhimg.com/v2-53b7b429eb9c7b8f15a99826d8f5f63e.jpg"],"type":0,"id":9654376,"ga_prefix":"103007","title":"在美国,赌绝症患者的性命,曾是一个暴利的灰色产业"},{"images":["https://pic1.zhimg.com/v2-c5d33bb44631d84e21b0d47a37b24210.jpg"],"type":0,"id":9654065,"ga_prefix":"103007","title":"创业很难吧?容易踩的坑,这份清单帮你列出了 11 点"},{"images":["https://pic2.zhimg.com/v2-a3a57207301cd3ea5705881216bef111.jpg"],"type":0,"id":9653907,"ga_prefix":"103006","title":"瞎扯 · 如何正确地吐槽"}]     * top_stories : [{"image":"https://pic4.zhimg.com/v2-8be8580bb386838153d2833099bdd7c7.jpg","type":0,"id":9654447,"ga_prefix":"103015","title":"电动车取代内燃机,除了技术之外更重要的是国家安全"},{"image":"https://pic1.zhimg.com/v2-c935e20adb8f99c51ffedd404c907c58.jpg","type":0,"id":9654376,"ga_prefix":"103007","title":"在美国,赌绝症患者的性命,曾是一个暴利的灰色产业"},{"image":"https://pic4.zhimg.com/v2-858034fe067a58c03b5d2b6f25d19b57.jpg","type":0,"id":9654362,"ga_prefix":"103008","title":"这群怪咖,用最土的方式拍出豆瓣评分最高的网剧"},{"image":"https://pic2.zhimg.com/v2-d617ae01c8508bb62366c87890271d7d.jpg","type":0,"id":9654263,"ga_prefix":"102917","title":"为什么有人会虐猫?"},{"image":"https://pic1.zhimg.com/v2-608ca624ba86b38e48ae17019e7a693c.jpg","type":0,"id":9654353,"ga_prefix":"102915","title":"天天喊「药丸」的日本动画,生意怎么越做越大了?"}]     */    private String date;    private List<StoriesBean> stories;    private List<TopStoriesBean> top_stories;    public String getDate() {        return date;    }    public void setDate(String date) {        this.date = date;    }    public List<StoriesBean> getStories() {        return stories;    }    public void setStories(List<StoriesBean> stories) {        this.stories = stories;    }    public List<TopStoriesBean> getTop_stories() {        return top_stories;    }    public void setTop_stories(List<TopStoriesBean> top_stories) {        this.top_stories = top_stories;    }    public static class StoriesBean {        /**         * images : ["https://pic4.zhimg.com/v2-b7f285f4d34102518689f648c6d1f303.jpg"]         * type : 0         * id : 9654447         * ga_prefix : 103015         * title : 电动车取代内燃机,除了技术之外更重要的是国家安全         */        private int type;        private int id;        private String ga_prefix;        private String title;        private List<String> images;        public int getType() {            return type;        }        public void setType(int type) {            this.type = type;        }        public int getId() {            return id;        }        public void setId(int id) {            this.id = id;        }        public String getGa_prefix() {            return ga_prefix;        }        public void setGa_prefix(String ga_prefix) {            this.ga_prefix = ga_prefix;        }        public String getTitle() {            return title;        }        public void setTitle(String title) {            this.title = title;        }        public List<String> getImages() {            return images;        }        public void setImages(List<String> images) {            this.images = images;        }    }    public static class TopStoriesBean {        /**         * image : https://pic4.zhimg.com/v2-8be8580bb386838153d2833099bdd7c7.jpg         * type : 0         * id : 9654447         * ga_prefix : 103015         * title : 电动车取代内燃机,除了技术之外更重要的是国家安全         */        private String image;        private int type;        private int id;        private String ga_prefix;        private String title;        public String getImage() {            return image;        }        public void setImage(String image) {            this.image = image;        }        public int getType() {            return type;        }        public void setType(int type) {            this.type = type;        }        public int getId() {            return id;        }        public void setId(int id) {            this.id = id;        }        public String getGa_prefix() {            return ga_prefix;        }        public void setGa_prefix(String ga_prefix) {            this.ga_prefix = ga_prefix;        }        public String getTitle() {            return title;        }        public void setTitle(String title) {            this.title = title;        }    }}

下面就看一下运行效果






原创粉丝点击