大笨狗

//Mainaction页面

package com.bwie.week0203;

import android.os.AsyncTask;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import com.bwie.week0203.adapter.NewsAdapter;
import com.bwie.week0203.bean.News;
import com.bwie.week0203.utils.HttpUtils;
import com.bwie.xlistview.XListView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private XListView xlvNews;
    private NewsAdapter adapter;
    private List<News.DataBeanX.DataBean> list;
    private int cuurect = 1;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xlvNews = findViewById(R.id.xlv_news);
        list = new ArrayList<>();
        adapter = new NewsAdapter(this,list);

        xlvNews.setAdapter(adapter);
        getdata(cuurect,false);
        xlvNews.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                cuurect = 1;
                getdata(cuurect,false);
            }

            @Override
            public void onLoadMore() {
                cuurect++;
                getdata(cuurect,true);

            }
        });

    }

    private void getdata(int cuurect, final boolean b) {

        if (cuurect<=2){
            xlvNews.setPullLoadEnable(true);
        }else {
            xlvNews.setPullLoadEnable(false);
        }

        new AsyncTask<String,Integer,String>(){

            @Override
            protected String doInBackground(String... strings) {
                return HttpUtils.getStringHttp(strings[0]);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson = new Gson();
                News news = gson.fromJson(s,News.class);
                if (news.getData().getData() != null){
                    if (!b){
                        list.clear();
                    }
                    list.addAll(news.getData().getData());
                    adapter.notifyDataSetChanged();
                }
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (b){
                            xlvNews.stopLoadMore();
                        }else{
                            xlvNews.stopRefresh();
                            xlvNews.setRefreshTime("刚刚");
                        }
                    }
                },2000);

            }

        }.execute("http://365jia.cn/news/api3/365jia/news/headline?page="+cuurect);
    }
}

//HttpUtils页面

 

package com.bwie.week0203.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by dell on 2018/9/8.
 */

public class HttpUtils {

    public static String getStringHttp(String urlString){
        String result = "";
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(3000);
            connection.connect();
            int code = connection.getResponseCode();

            if (code ==200){
                InputStream is = connection.getInputStream();
                result = getStringUrlHttp(is);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    private static String getStringUrlHttp(InputStream is) {
        String result = "";
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int length = -1;
        byte[] buffer = new byte[1024];
        try {
            while ((length = is.read(buffer,0,buffer.length)) != -1){
                baos.write(buffer,0,length);
                baos.flush();
            }
            result = baos.toString();
            baos.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }
}

//Application页面

 

 

package com.bwie.week0203.applicetion;

import android.app.Application;
import android.graphics.Bitmap;
import android.os.Environment;

import com.bwie.week0203.R;
import com.nostra13.universalimageloader.cache.disc.DiskCache;
import com.nostra13.universalimageloader.cache.disc.impl.ext.LruDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.io.File;
import java.io.IOException;

/**
 * Created by dell on 2018/9/8.
 */

public class BeansApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        File file = null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            File rootSD = Environment.getExternalStorageDirectory();
            file = new File(rootSD,"imgs");
            if(!file.exists()){
                file.mkdirs();
            }
        }

        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheOnDisk(true)
                .cacheInMemory(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageForEmptyUri(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher)
                .build();

        DiskCache diskCache = null;

        try {
            diskCache = new LruDiskCache(file,new Md5FileNameGenerator(),50*1024*1024);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
                .diskCache(diskCache)
                .defaultDisplayImageOptions(options)
                .threadPoolSize(3)
                .build();
        ImageLoader.getInstance().init(configuration);
    }
}

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值