从网络获取图片资源缓存到本地,第二次进入直接从本地加载。

本文介绍了一个基于Android的列表刷新和加载更多的实现方式,通过XListView组件实现下拉刷新和上拉加载的功能,并使用AsyncTask进行后台数据加载。
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {

private XListView xlv;
    private MyxLvAdapter adapter;
    private boolean bo = false;
    private int index = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
        xlv.setPullRefreshEnable(true);
        xlv.setPullLoadEnable(true);
        xlv.setXListViewListener(this);
        initData(index);
    }

    private void initUI() {
        xlv = (XListView) findViewById(R.id.xlv);
    }


    //下拉刷新
    @Override
    public void onRefresh() {
        bo = true;
        ++index;
        initData(index);
        xlv.stopRefresh(true);
    }


    //上拉加载更多
    @Override
    public void onLoadMore() {
        bo = false;
        ++index;
        initData(index);
        xlv.stopLoadMore();
    }

    private void initData(int index) {
        new AsyncTask<String, Void, String>() {
            private ProgressDialog dialog;
            //在异步任务执行的时候 最先执行的方法
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                dialog = new ProgressDialog(MainActivity.this);
                dialog.setMessage("luluilu加载中");
                dialog.show();
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                dialog.dismiss();
                if (s != null) {
                    Gson gson = new Gson();
                    Bean bean = gson.fromJson(s, Bean.class);
                    List<Bean.ResultBean.DataBean> dataBeanList = bean.getResult().getData();
                    if (adapter == null) {
                        adapter = new MyxLvAdapter(MainActivity.this, dataBeanList);
                        xlv.setAdapter(adapter);
                    } else {
                        adapter.addMore(dataBeanList, bo);
                        adapter.notifyDataSetChanged();
                    }
                }
            }
            @Override
            protected String doInBackground(String... params) {
                try {
                    String path = params[0];
                    URL url = new URL(path);
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("GET");
                    httpURLConnection.setReadTimeout(5000);
                    httpURLConnection.setConnectTimeout(5000);

                    if (httpURLConnection.getResponseCode() == 200) {
                        InputStream is = httpURLConnection.getInputStream();
                        String json = StreamTool.readNetWork(is);

                        FileWriter data = new FileWriter(new File(Environment.getExternalStorageDirectory(), "data.json"));
                        data.write(json);
                        data.flush();
                        data.close();
                         return json;
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute("http://apis.juhe.cn/cook/query?key=faecc23d16cbd9bef7ce6043b7d612b3&menu=水煮肉&pn=" + index);

    }
}
StreamTool:

/**
 * Created by xyn on 2017/4/25.
 */

public class StreamTool {

    public static String readNetWork(InputStream inputStream) throws IOException {

        
ByteArrayOutputStream boas = new ByteArrayOutputStream();
        
byte[] bytes = new byte[1024];
        
int len=0;

       
 while ((len = inputStream.read(bytes)) !=-1){
            
boas.write(bytes,0,len);
        }
        
return boas.toString();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值