package com.bwie.imageloadercache;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView lv;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
boolean b = getnetState(getApplicationContext());
if(b){
System.out.println("有网网网网网网网网网网网网网网网网网网网网网网网");
loadData("http://japi.juhe.cn/health_knowledge/infoList?key=0efb839c8e13e91a0e928f8164048a5d");
}else{
System.out.println("没网网网网网网网网网网网网网网网网网网网网网网网");
// lv.setAdapter(new Myadapter(list));
try {
File file = new File(getCacheDir(), "info.txt");
InputStream is= new FileInputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes)) != -1) {
baos.write(bytes, 0, len);
}
is.close();
baos.close();
String s=baos.toString();
Gson gson = new Gson();
HealthInfo healthInfo = gson.fromJson(s, HealthInfo.class);
List<HealthInfo.ResultBean.DataBean> list = healthInfo.getResult().getData();
lv.setAdapter(new Myadapter(list));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void loadData(String path) {
System.out.println("******************loadData");
new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
try {
System.out.println("******************doInBackground");
HttpURLConnection connection = (HttpURLConnection) new URL(params[0]).openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("******************HTTP_OK");
InputStream is = connection.getInputStream();
File file = new File(getCacheDir(),"info.txt");
OutputStream os = new FileOutputStream(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes)) != -1) {
baos.write(bytes, 0, len);
os.write(bytes, 0, len);
}
os.close();
is.close();
baos.close();
System.out.println("写完完完完完完完完完完完完完");
return baos.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
System.out.println("******************onPostExecute");
super.onPostExecute(s);
if (s != null) {
Gson gson = new Gson();
HealthInfo healthInfo = gson.fromJson(s, HealthInfo.class);
List<HealthInfo.ResultBean.DataBean> list = healthInfo.getResult().getData();
lv.setAdapter(new Myadapter(list));
}
}
}.execute(path);
}
class Myadapter extends BaseAdapter {
DisplayImageOptions options;
List<HealthInfo.ResultBean.DataBean> list;
public Myadapter(List<HealthInfo.ResultBean.DataBean> list) {
this.list = list;
options = new DisplayImageOptions.Builder()
.cacheOnDisk(true)
.cacheInMemory(true)
.build();
}
@Override
public int getCount() {
return list != null ? list.size() : 0;
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(getApplicationContext());
ImageLoader.getInstance().displayImage(list.get(position).getImg(), imageView, options);
return imageView;
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
boolean getnetState(Context context) {
ConnectivityManager mannager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = mannager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}