1.Gson
2.universal-image-loader-1.9.3-with-sources.jar
3.File–>Project Stucture–>app后面脑补
4.File–>New –>Import Module
5.清单文件 res下面
6.添加依赖库 Android下 Gradle app
compile ‘com.youth.banner:banner:1.4.9’layout
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
tools:context="bwei.com.example.lenovo.httpurlconnection.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.youth.banner.Banner
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/banner"
>com.youth.banner.Banner>
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pull_lv"
ptr:ptrDrawable="@drawable/default_ptr_flip"
ptr:ptrAnimationStyle="flip"
ptr:ptrHeaderBackground="#383838"
ptr:ptrHeaderTextColor="#FFFFFF" > com.handmark.pulltorefresh.library.PullToRefreshListView>
LinearLayout>
<FrameLayout
android:id="@+id/drawer"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#f79"
>FrameLayout>
android.support.v4.widget.DrawerLayout>
item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="神经病"
android:layout_alignBottom="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:layout_toEndOf="@+id/imageView"
android:layout_marginLeft="47dp"
android:layout_marginStart="47dp"
android:layout_marginBottom="10dp" />
RelativeLayout>
left.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00">
LinearLayout>
drawable
ic_empty.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
selector>
ic_error.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
selector>
ic_stub.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
selector>
MainActivity
package bwei.com.example.lenovo.httpurlconnection;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.youth.banner.Banner;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
import bwei.com.example.lenovo.httpurlconnection.Utils.Mytask;
public class MainActivity extends AppCompatActivity {
private List sjlist = new ArrayList<>();
private PullToRefreshListView pull;
int pageIndex =1;
private Handler myHandler = new Handler();
private List newslist;
private MyAdapter adapter;
private Banner banner;
private List list = new ArrayList();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pull = (PullToRefreshListView) findViewById(R.id.pull_lv);
banner = (Banner) findViewById(R.id.banner);
Mytask mytask = new Mytask(new Mytask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson = new Gson();
Bean bean = gson.fromJson(jsonstr, Bean.class);
newslist = bean.getNewslist();
for (int i = 0;i< newslist.size();i++){
list.add(newslist.get(i).getPicUrl());
}
//设置图片加载器
banner.setImageLoader(new MImageLoader());
//设置含有图片路径的集合
banner.setImages(list);
//设置延迟时间
banner.setDelayTime(3000);
//是否自动轮播
banner.isAutoPlay(true);
//开启
banner.start();
}
});
mytask.execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=");
// getSupportFragmentManager().beginTransaction().add(R.id.drawer,new FragmentLeft()).commit();
initLv();
initData();
}
private void initData() {
Mytask myTask = new Mytask(new Mytask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson = new Gson();
Bean bean = gson.fromJson(jsonstr, Bean.class);
List newslist = bean.getNewslist();
sjlist.addAll(newslist);
Log.d("zzz",newslist+"newlist");
setAdapter();
}
});
myTask.execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page="+pageIndex);
}
private void initLv() {
//设置刷新模式 ,both代表支持上拉和下拉,pull_from_end代表上拉,pull_from_start代表下拉
pull.setMode(PullToRefreshBase.Mode.BOTH);
/*这里通过getLoadingLayoutProxy 方法来指定上拉和下拉时显示的状态的区别,第一个true 代表下来状态 ,第二个true 代表上拉的状态
如果想区分上拉和下拉状态的不同,可以分别设置*/
ILoadingLayout startLabels = pull.getLoadingLayoutProxy(true, false);
startLabels.setPullLabel("下拉刷新");
startLabels.setRefreshingLabel("正在拉");
startLabels.setReleaseLabel("放开刷新");
ILoadingLayout endLabels = pull.getLoadingLayoutProxy(false, true);
endLabels.setPullLabel("上拉刷新");
endLabels.setRefreshingLabel("正在载入...");
endLabels.setReleaseLabel("放开刷新...");
/*如果Mode设置成Mode.BOTH,需要设置刷新Listener为OnRefreshListener2,并实现onPullDownToRefresh()、onPullUpToRefresh()两个方法。
如果Mode设置成Mode.PULL_FROM_START或Mode.PULL_FROM_END,需要设置刷新Listener为OnRefreshListener,同时实现onRefresh()方法。
当然也可以设置为OnRefreshListener2,但是Mode.PULL_FROM_START的时候只调用onPullDownToRefresh()方法,Mode.PULL_FROM_END的时候只调用onPullUpToRefresh()方法.
加载数据完成后 必须 调用下 onRefreshComplete() 完成关闭 header,footer视图
*/
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
@Override
public void onPullDownToRefresh(PullToRefreshBase refreshView) {//下拉刷新的回调
//下拉刷新的数据,显示在listview列表的最上面
addtoTop();
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
//刷新完成,必须在异步下完成
pull.onRefreshComplete();
//刷新适配器
setAdapter();
}
},1000);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase refreshView) {//上拉加载的回调
//加载更多的数据,添加到集合列表的最后面
addtoBottom();
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
//刷新完成,必须在异步下完成
pull.onRefreshComplete();
//刷新适配器
setAdapter();
}
},1000);
}
});
}
//下拉加载
public void addtoTop(){
Mytask myTask=new Mytask(new Mytask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson=new Gson();
Bean bean=gson.fromJson(jsonstr, Bean.class);
//添加到大集合对象中
List newslist = bean.getNewslist();
sjlist.addAll(0,newslist);
//设置适配器
setAdapter();
Log.d("z1",newslist+"newlist");
}
});
myTask.execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1" );
}
//上拉刷新
public void addtoBottom(){
pageIndex++;
Mytask myTask=new Mytask(new Mytask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson=new Gson();
Bean bean=gson.fromJson(jsonstr, Bean.class);
List newslist = bean.getNewslist();
//添加到大集合对象中
sjlist.addAll(0,newslist);
Log.d("zzzz2",newslist+"newlist");
//设置适配器
setAdapter();
}
});
myTask.execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page="+pageIndex);
}
public void setAdapter(){
if(adapter==null){
adapter=new MyAdapter();
pull.setAdapter(adapter);
}else{
adapter.notifyDataSetChanged();
}
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return sjlist.size();
}
@Override
public Object getItem(int i) {
return sjlist.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHodler holder;
if (view==null){
holder = new ViewHodler();
view= View.inflate(MainActivity.this, R.layout.item, null);
holder.imageView = (ImageView) view.findViewById(R.id.imageView);
holder.tv = (TextView) view.findViewById(R.id.tv);
view.setTag(holder);
}else{
holder = (ViewHodler) view.getTag();
holder.tv.setText(sjlist.get(i).getTitle());
ImageLoader.getInstance().displayImage(sjlist.get(i).getPicUrl(),holder.imageView);
}
return view;
}
class ViewHodler{
private ImageView imageView;
private TextView tv;
}
}
}
Bean
package bwei.com.example.lenovo.httpurlconnection;
import java.util.List;
/**
* Created by lenovo on 2017/10/25.
*/
public class Bean {
/**
* code : 200
* msg : success
* newslist : [{"ctime":"2017-10-25","title":"西方或仍在迷茫,中国已八个\u201c明确\u201d","description":"海外网","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-55990598.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=447×tamp=1508889630&signature=83-E1YbfzVxRLIRiWKtnbvDphVmVfw*QU5m0*DKPkyjdSgCvpHCS7Pti6ncE5MzSL1rDdh*5JgaDqmh2dMQ*GDKsgCYLx-tJY0*vWcHt70Y="},{"ctime":"2017-10-25","title":"房地产新时代:房屋租赁开启蓝海","description":"第一财经网","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-55990601.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=447×tamp=1508889630&signature=FjP*h0If2SxbRoT-atlihCyDWY6-UpQ-eDjlw22rspRvyBvrHItLof6SvRBpla50jr4t8Hw7C8SddMhEdJyazQchkh-qi6ivzyYTL2-gzd0="},{"ctime":"2017-10-25","title":"历史的选择 人民的选择 全党的选择","description":"海外网","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-37632835.static/640","url":"https://mp.weixin.qq.com/s?src=16&ver=447×tamp=1508889629&signature=uHTwsd4NnOGMd4RaGkgL0*S7DgFBhepEY9Ljrw1*dLMt1G-BVqmL4y3vZVJiEjA6XXjZy4Sie*p1wARsngAf6OOs9y1o46G*bbpLiWMgU5s="},{"ctime":"2017-10-25","title":"CBA\u201c姚\u201d赛季开场,职业化走出飞升曲线","description":"第一财经网","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-55990724.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=447×tamp=1508889629&signature=wNkKRyMyIRiRXEECfCMVa3l0ZBl4OeYZ172StUtlM*WdJ5fPaH*bT5B0wQWQd6HGeMKMhUF0lNFWpQjk85Aya5050Pzp5JEfpAtTRoS7ZPU="},{"ctime":"2017-10-25","title":"管你速度再快,没刹车只能抓瞎","description":"大家车言论","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-55990563.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=447×tamp=1508889629&signature=alOTmOeX54JAFXi7VWMZl*No-qcdIfJJE4-LJsw5wb5vQx6FPEv5VYQpokXn50Lj0wpve8xfyqd-vlBaEr5B*y3a1Rk1d*2xiczaJWK00kE="},{"ctime":"2017-10-25","title":"羊身上一物治耳鸣耳聋,一千四百年前被名医相中,终于流传开","description":"微门诊","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-55990453.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=447×tamp=1508889629&signature=u-GpZMcMn1hwQondm7WXD2QrecDezYHHtz-wHaGuLGHNyk49PCxky5r2Cj*fxgRt30FQca6cnXXynCt78g49qIA24DTmW-92XgKVdipyg6g="},{"ctime":"2017-10-25","title":"治鼻炎,此方是\u201c鼻祖\u201d,冬天最常用,流行七百多年","description":"微门诊","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-55990454.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=447×tamp=1508889628&signature=nIjbIU865ZL*0xxHgNHIzClHhREY3AKtiSkcAv*4cT2zOFdxJsBlkbUKl4pRLfPkJ8xb8475IqyMZjYHevYFB6t7LyafeoEZTrbu6lfxsw0="},{"ctime":"2017-10-25","title":"买房容易验房难!这些陷阱你要注意","description":"腾讯财经","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-55990475.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=447×tamp=1508889628&signature=vKw9FtCbTJg-QL0uTRmzzh079IX2gdSzxlqarrAhQhUjaNL-mGx4iO9G-Ry0EouzNWcP6reDmOcL7tqufr8W730XSMKRZqJ*zNoQHgrF3g8="},{"ctime":"2017-10-25","title":"日本又双叒出造假丑闻!这次连\u201c神户牛肉\u201d也沦陷了?","description":"腾讯财经","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-55990473.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=447×tamp=1508889628&signature=vttOVoCgI0W0nuvt1YVrou0N0ZJ0176G3yPZXpPB32bjQvmLrGVEsCcL4yZlpr*N-guO-kFDJUkqqHZo-*VUVMmSTcYWvK5cS5j*YxvANNk="},{"ctime":"2017-10-25","title":"这种理财产品在中国终于出现了! 1块钱也可投资房产?","description":"腾讯财经","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-55990496.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=447×tamp=1508889627&signature=BcFgnd0Nk9Y2HSbAS9QTBewByeUDLpx94dxYbH2rTs9I2*T5cGppSO4*c1r1*LYCQbtam*mr9TnGkVE-BBpQgQJtq6eESm2wGqW0Sm4pGZg="}]
*/
private int code;
private String msg;
private List newslist;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public List getNewslist() {
return newslist;
}
public void setNewslist(List newslist) {
this.newslist = newslist;
}
public static class NewslistBean {
/**
* ctime : 2017-10-25
* title : 西方或仍在迷茫,中国已八个“明确”
* description : 海外网
* picUrl : https://zxpic.gtimg.com/infonew/0/wechat_pics_-55990598.jpg/640
* url : https://mp.weixin.qq.com/s?src=16&ver=447×tamp=1508889630&signature=83-E1YbfzVxRLIRiWKtnbvDphVmVfw*QU5m0*DKPkyjdSgCvpHCS7Pti6ncE5MzSL1rDdh*5JgaDqmh2dMQ*GDKsgCYLx-tJY0*vWcHt70Y=
*/
private String ctime;
private String title;
private String description;
private String picUrl;
private String url;
public String getCtime() {
return ctime;
}
public void setCtime(String ctime) {
this.ctime = ctime;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
}
ImageLoder
package bwei.com.example.lenovo.httpurlconnection;
import android.app.Application;
import android.graphics.Bitmap;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.io.File;
/**
* Created by lenovo on 2017/10/25.
*/
public class ImageLoder extends Application {
@Override
public void onCreate() {
super.onCreate();
//storage/sdcard/data//cache
//sd/mycache/
File cacheDir=this.getExternalCacheDir();//自定义缓存路径
ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this)
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))//设置内存缓存区大小
.memoryCacheSize(10 * 1024 * 1024)//设置缓存区大小
.memoryCacheExtraOptions(520,520)//缓存图片最大的宽度与高度 px
.diskCacheSize(50*1024*1024)//设置sd卡缓存的空间大小
.diskCacheFileNameGenerator(new Md5FileNameGenerator())//sd卡缓存图片的命名 使用md5加密方式
.diskCache(new UnlimitedDiscCache(cacheDir))//自定义sd卡的缓存路径
.diskCacheFileCount(100)//缓存文件的最大数量
.writeDebugLogs()//写入日志
.threadPoolSize(3)//线程
.build();
//对imageLoader进行初使化
ImageLoader.getInstance().init(configuration);
}
public final static DisplayImageOptions getDefaultDisplayOption() {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true)//进行内存缓存
.cacheOnDisk(true)//进行sd卡缓存
.showImageOnLoading(R.drawable.ic_stub) // 设置正在下载中的图片
.showImageForEmptyUri(R.drawable.ic_empty) //没有请求地址时
.showImageOnFail(R.drawable.ic_error)//下载错误时
.bitmapConfig(Bitmap.Config.RGB_565).build();
//设置图片质量build(); // 创建配置过得DisplayImageOption对象
return options;
}
}
Utils
Mytask
package bwei.com.example.lenovo.httpurlconnection.Utils;
import android.os.AsyncTask;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
/**
* Created by lenovo on 2017/10/25.
*/
public class Mytask extends AsyncTask<String,Void,String>{
//申请一个接口类对象
private Icallbacks icallbacks;
//将无参构造设置成私有的,使之在外部不能够调用
private Mytask(){}
//定义有参构造方法
public Mytask(Icallbacks icallbacks) {
this.icallbacks = icallbacks;
}
@Override
protected String doInBackground(String... strings) {
String str="";
try {
//使用HttpUrlConnection
URL url=new URL(strings[0]);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
if(connection.getResponseCode()==200){
InputStream inputStream=connection.getInputStream();
//调用工具类中的静态方法
str=StreamToString.streamToStr(inputStream,"utf-8");
}
} catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return str;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//解析,封装到bean,更新ui组件
icallbacks.updateUiByjson(s);
}
//定义一个接口
public interface Icallbacks{
/**
* 根据回传的json字符串,解析并更新页面组件
* @param jsonstr
*/
void updateUiByjson(String jsonstr);
}
}
StreamToString
package bwei.com.example.lenovo.httpurlconnection.Utils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Created by lenovo on 2017/10/25.
*/
public class StreamToString {
public static String streamToStr(InputStream inputStream, String chartSet){
StringBuilder builder=new StringBuilder();
try {
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream,chartSet));
String con;
while ((con=br.readLine())!=null){
builder.append(con);
}
br.close();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
MImageLoader
package bwei.com.example.lenovo.httpurlconnection;
import android.content.Context;
import android.widget.ImageView;
import com.youth.banner.loader.ImageLoader;
/**
* Created by lenovo on 2017/10/25.
*/
public class MImageLoader extends ImageLoader{
public void displayImage(Context context, Object path, ImageView imageView) {
com.nostra13.universalimageloader.core.ImageLoader instance = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
instance.displayImage((String) path, imageView);
}
}
FragmentLeft
package bwei.com.example.lenovo.httpurlconnection;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by lenovo on 2017/10/25.
*/
class FragmentLeft extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(),R.layout.left,null);
return view;
}
}