public class Myapplication extends Application { @Override public void onCreate() { super.onCreate();
File cacheDir = StorageUtils.getOwnCacheDirectory(this,
Environment.getExternalStorageDirectory().getPath());
Log.e(" 22222222222", "onCreate: 缓存路径-------------------》"+Environment.getExternalStorageDirectory().getPath());
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
.threadPoolSize(3)//配置线程数量
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))//内存缓存图片 2M
.diskCache(new UnlimitedDiskCache(cacheDir))//配饰sdcard缓存路径
.diskCacheSize(50 * 1024 * 1024)//sdcard上缓存50M的图片
.diskCacheFileCount(100)//缓存文件的数量 100个
.diskCacheFileNameGenerator(new Md5FileNameGenerator())
.build();
//配置缓存选项
ImageLoader.getInstance().init(configuration);
}
}
/**
- com.example.text2.Fragment
- 徐世辉 1503A
-
<p>
- 2017/5/3 */
public class ImageAsy extends AsyncTask<String,Void,Bitmap> { public interface ImageCallback{
void claaback(Bitmap bitmap);
}
private ImageCallback imageCallback;
public ImageAsy(ImageCallback imageCallback) {
this.imageCallback = imageCallback;
}
[@Override](https://my.oschina.net/u/1162528)
protected Bitmap doInBackground(String... params) {
try {
String path = params[0];
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5*1000);
connection.setReadTimeout(5*1000);
//服务器响应
int code = connection.getResponseCode();
if(code == HttpURLConnection.HTTP_OK){//判断服务器是否连接成功并相应
//图片流
InputStream is = connection.getInputStream();
//将图片流转化成Bitmap位图,返回
return BitmapFactory.decodeStream(is);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
[@Override](https://my.oschina.net/u/1162528)
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (bitmap!=null){
imageCallback.claaback(bitmap);
}}
}
/**
- com.example.text2.Fragment
- 徐世辉 1503A
-
<p>
- 2017/5/3 */
public class MyAdapter extends BaseAdapter{ private Context context; private List<Bean1.DataBean> data; private final DisplayImageOptions displayImageOptions;
public MyAdapter(Context context, List<Bean1.DataBean> data) {
this.context = context;
this.data = data;
//是否内存缓存
//是否sdcard缓存 //构建图片缓存的选项 displayImageOptions = new DisplayImageOptions.Builder() .cacheInMemory(true)//是否内存缓存 .cacheOnDisk(true)//是否sdcard缓存 .build(); }
[@Override](https://my.oschina.net/u/1162528)
public int getCount() {
return data.size();
}
[@Override](https://my.oschina.net/u/1162528)
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHandler viewHandler;
if (convertView==null){
viewHandler=new ViewHandler();
convertView=View.inflate(context, R.layout.item,null);
viewHandler.imageView= (ImageView) convertView.findViewById(R.id.item_image);
viewHandler.textView= (TextView) convertView.findViewById(R.id.item_text);
viewHandler.textView1= (TextView) convertView.findViewById(R.id.item_text2);
convertView.setTag(viewHandler);
}else {
viewHandler= (ViewHandler) convertView.getTag();
}
viewHandler.textView.setText(data.get(position).getTITLE());
viewHandler.textView1.setText(data.get(position).getSUBTITLE());
if (data.get(position).getIMAGEURL()!=null) {
//viewHandler.getimage(data.get(position).getIMAGEURL());
ImageLoader.getInstance().displayImage(data.get(position).getIMAGEURL(),viewHandler.imageView,displayImageOptions);
}else {
viewHandler.imageView.setVisibility(View.GONE);
}
return convertView;
}
class ViewHandler{
private ImageView imageView;
private TextView textView,textView1;
public void getimage(String url){
ImageAsy imageAsy=new ImageAsy(new ImageAsy.ImageCallback() {
@Override
public void claaback(Bitmap bitmap) {
if (bitmap!=null){
imageView.setImageBitmap(bitmap);
}
}
});
imageAsy.execute(url);
}}
}
/**
- com.example.text2.Fragment
- 徐世辉 1503A
-
<p>
- 2017/5/3 */
public class Getitem { public String getitems(String url,int channelId,int startNum) { try { URL url1=new URL(url); HttpURLConnection httpURLConnection= (HttpURLConnection) url1.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setReadTimeout(5000); httpURLConnection.setConnectTimeout(5000);
// 要发送给服务器的请求的实体内容
OutputStream os = httpURLConnection.getOutputStream();
os.write(("channelId="+channelId+"&startNum="+startNum).getBytes());
PrintWriter writer = new PrintWriter(os);
//把数据刷出去
writer.flush();
int code= httpURLConnection.getResponseCode();
if (code==200){
InputStream inputStream= httpURLConnection.getInputStream();
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
byte[] b =new byte[1024];
int read = 0;
while ((read = inputStream.read(b))!= -1){
byteArrayOutputStream.write(b,0,read);
}
return byteArrayOutputStream.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
/**
* 获取当前的网络状态 :没有网络0:WIFI网络1:移动数据2
*
* @param context
* @return
*/
public static int getAPNType(Context context) {
int netType = 0;
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null) {
return netType;
}
int nType = networkInfo.getType();
if (nType == ConnectivityManager.TYPE_WIFI) {
netType = 1;// wifi
} else if (nType == ConnectivityManager.TYPE_MOBILE) {
//有移动数据
//int nSubType = networkInfo.getSubtype();
/*TelephonyManager mTelephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
&& !mTelephony.isNetworkRoaming()) {
netType = 2;// 3G
} else {
netType = 3;// 2G
}*/
netType = 2;
}
return netType;
}
}
/**
- com.example.text2.Fragment
- 徐世辉 1503A
-
<p>
- 2017/5/3 */
public class Fr1 extends Fragment {
private ListView listView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fr1,container,false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView = (ListView) getView().findViewById(R.id.fr1_lv);
int connType = Getitem.getAPNType(getActivity());
if (connType == 1){
Log.e("onActivityCreated: ","是wifi网络" );
getids("http://www.93.gov.cn/93app/data.do");
}else if (connType >1){
Log.e("onActivityCreated: ","是移动网络" );
AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
.setTitle("移动网络继续访问")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getids("http://www.93.gov.cn/93app/data.do");
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(),"将不再移动数据下访问",Toast.LENGTH_SHORT).show();
}
}).show();
}else {
Log.e("onActivityCreated: ","没有网络" );
AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
.setTitle("没有网络")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(),"等待网络链接",Toast.LENGTH_SHORT).show();
try {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
File file = new File(Environment.getExternalStorageDirectory(),"66.txt");
if(file.exists())
try {
FileInputStream fileInputStream= new FileInputStream(file);
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
byte[] b =new byte[1024];
int read = 0;
while ((read = fileInputStream.read(b))!= -1){
byteArrayOutputStream.write(b,0,read);
}
String ll= byteArrayOutputStream.toString();
Gson gson=new Gson();
Bean1 bean=gson.fromJson(ll, Bean1.class);
List<Bean1.DataBean> data1= bean.getData();
MyAdapter m= new MyAdapter(getActivity(),data1);
listView.setAdapter(m);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
} }
catch (Exception e) {
e.printStackTrace();
}
}
}).show();
}
}
public void getids(String url){
new AsyncTask<String,Void,String>(){
@Override
protected String doInBackground(String... params) {
String p=params[0];
return new Getitem().getitems(p,0,0);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.e( "onPostExecute: ","发那个地方公开就爱看上到几点"+s );
Gson gson=new Gson();
try {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
File file = new File(Environment.getExternalStorageDirectory(),"66.txt");
if(file.exists())
{file.createNewFile();
}
FileOutputStream fileOutputStream= new FileOutputStream(file);
fileOutputStream.write(s.getBytes());
fileOutputStream.close();
} }
catch (IOException e) {
e.printStackTrace();
}
Bean1 bean=gson.fromJson(s, Bean1.class);
List<Bean1.DataBean> data= bean.getData();
MyAdapter m= new MyAdapter(getActivity(),data);
listView.setAdapter(m);
}
}.execute(url);
}
}