这是曾经做过的一个小Demo,主要是请求网络数据时,将图片进行缓存,再次登陆时使用本地缓存的数据
使用的时xlistview实现上拉加载更多,下拉刷新数据
不足之处,还请见谅
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {
private static final String TAG = "MainActivity";
private XListView lv;
private int index=1;
private MyListViewAdapter adapter;
private boolean b = true;
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (XListView) findViewById(R.id.xlv);
lv.setPullLoadEnable(true);
lv.setXListViewListener(this);
//得到存储的file文件
File file = new File(Environment.getExternalStorageDirectory(), "json.txt");
//如果存在,将她转换成输入流,并为其设置适配器
if (file.exists()){
try {
InputStream is = new FileInputStream(file);
//StreamToos将字符串转换成输入流的工具类,将输入流转换成字符串,在这里就不演示了
String s = StreamToos.readFromNetWork(is);
setListViewAdapter(s);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}else{
getData(index);
}
}
//设置xlistview的适配器
public void setListViewAdapter(String s){
if (s!=null){
//判断字符串不为空,通过Gson,创建与其对应的封装类
Gson gson = new Gson();
Foods foods = gson.fromJson(s, Foods.class);
List<Foods.ResultBean.DataBean> list = foods.getResult().getData();
//得到集合,设置适配器
if (adapter == null){
adapter = new MyListViewAdapter(MainActivity.this,list);
lv.setAdapter(adapter);
}else{
//addMore()是适配器类的一个方法,主要是向适配器适配的集合添加数据
//参数1:添加的数据
//参数2:传递一个标记,如果这个值为true,就往集合的前面添加数据,下拉刷新时使用
//如果为false,往后面添加数据,上拉加载时使用
//适配器的详细信息请看上一篇博客,xlistview的简单使用
adapter.addMore(foods.getResult().getData(),b);
adapter.notifyDataSetChanged();
}
}
}
private void getData(int ind) {
new AsyncTask<String,Void,String>(){
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("玩命加载中");
dialog.show();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
dialog.dismiss();
//设置 xlistview的适配器
setListViewAdapter(s);
}
@Override
protected String doInBackground(String... params) {
try {
String path = params[0];
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
if (connection.getResponseCode() == 200){
InputStream is = connection.getInputStream();
String json = StreamToos.readFromNetWork(is);
//将json串存储到本地
File file = new File(Environment.getExternalStorageDirectory(), "json.txt");
if (!file.exists()){
file.createNewFile();
}
OutputStream os = new FileOutputStream(file);
os.write(json.getBytes());
Log.e(TAG, "存储成功");
os.close();
return json;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.execute("http://apis.juhe.cn/cook/query?key=de558ae7e2e2a24fc1f013fbb3327ae1&rn=10&menu=红烧肉&pn="+ind);
}
//下拉刷新
@Override
public void onRefresh() {
b=false;
++index;
getData(index);
lv.stopRefresh(true);
}
//上拉加载
@Override
public void onLoadMore() {
b=true;
++index;
getData(index);
lv.stopLoadMore();
}
}