public class MainActivity extends AppCompatActivity {
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
adlist = (List<InfoBean.DataBean.AdlistBean>) msg.obj;
MyAdapter adapter = new MyAdapter();
lv.setAdapter(adapter);
}
};
private ListView lv;
private List<InfoBean.DataBean.AdlistBean> adlist;
private DisplayImageOptions options;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
sp = getSharedPreferences("config", MODE_PRIVATE);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher)
.cacheInMemory(true)
.cacheOnDisk(true)
.build();
if (sp.getString("json",null)==null){
Toast.makeText(this, "第一次", Toast.LENGTH_SHORT).show();
new Thread(){
@Override
public void run() {
super.run();
String json = NetWork.getJson();
sp.edit().putString("json",json).commit();
getGsonData(json);
// Toast.makeText(MainActivity.this,json,Toast.LENGTH_LONG).show();
}
}.start();
}else{
Toast.makeText(this, "第二次有缓存了", Toast.LENGTH_SHORT).show();
String json=sp.getString("json",null);
getGsonData(json);
}
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return adlist.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null){
holder = new ViewHolder();
convertView=View.inflate(MainActivity.this,R.layout.list_item,null);
holder.iv= (ImageView) convertView.findViewById(R.id.iv);
holder.tv= (TextView) convertView.findViewById(R.id.tv);
convertView.setTag(holder);
}else {
holder= (ViewHolder) convertView.getTag();
}
ImageLoader.getInstance().displayImage(adlist.get(position).getImg(),holder.iv,options);
holder.tv.setText(adlist.get(position).getName());
return convertView;
}
class ViewHolder{
ImageView iv;
TextView tv;
}
}
private void getGsonData(String json) {
Gson gson = new Gson();
InfoBean infoBean = gson.fromJson(json, InfoBean.class);
List<InfoBean.DataBean.AdlistBean> adlist = infoBean.getData().getAdlist();
Message message = new Message();
message.obj=adlist;
handler.sendMessage(message);
}
}
//《2、》进行网络请求
public class NetWork {
public static String getJson(){
String path="http://www.meirixue.com/api.php?c=index&a=index";
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int responseCode = connection.getResponseCode();
byte[] arr = new byte[1024];
int len;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (responseCode == 200) {
InputStream inputStream = connection.getInputStream();
while ((len=inputStream.read(arr))!=-1){
baos.write(arr,0,len);
}
return baos.toString();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
//<3>Application配置图片
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this)
.threadPoolSize(3)
.memoryCacheSize(1024*2)
.build();
ImageLoader.getInstance().init(configuration);
}
}