多条目加载解析方法,读取和图片缓存
来源:互联网 发布:淘宝小号绑定大号 编辑:程序博客网 时间:2024/05/29 06:46
package animtest.com.example.e531.listview_requestdata.Utils;
import android.os.AsyncTask;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* 使用AsyncTask+HttpURLConnection请求数据
* Created by e531 on 2017/10/12.
*/
public class MyTask extends AsyncTask<String,Void,String> {
* 使用AsyncTask+HttpURLConnection请求数据
* Created by e531 on 2017/10/12.
*/
public class MyTask extends AsyncTask<String,Void,String> {
//申请一个接口类对象
private Icallbacks icallbacks;
private Icallbacks icallbacks;
//将无参构造设置成私有的,使之在外部不能够调用
private MyTask(){}
private MyTask(){}
//定义有参构造方法
public MyTask(Icallbacks icallbacks) {
this.icallbacks = icallbacks;
}
public MyTask(Icallbacks icallbacks) {
this.icallbacks = icallbacks;
}
@Override
protected String doInBackground(String... params) {
String str="";
protected String doInBackground(String... params) {
String str="";
try {
//使用HttpUrlConnection
URL url=new URL(params[0]);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
//使用HttpUrlConnection
URL url=new URL(params[0]);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
if(connection.getResponseCode()==200){
InputStream inputStream=connection.getInputStream();
//调用工具类中的静态方法
str=StreamToString.streamToStr(inputStream,"utf-8");
}
InputStream inputStream=connection.getInputStream();
//调用工具类中的静态方法
str=StreamToString.streamToStr(inputStream,"utf-8");
}
} catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return str;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//解析,封装到bean,更新ui组件
icallbacks.updateUiByjson(s);
protected void onPostExecute(String s) {
super.onPostExecute(s);
//解析,封装到bean,更新ui组件
icallbacks.updateUiByjson(s);
}
//定义一个接口
public interface Icallbacks{
/**
* 根据回传的json字符串,解析并更新页面组件
* @param jsonstr
*/
void updateUiByjson(String jsonstr);
}
}
//定义一个接口
public interface Icallbacks{
/**
* 根据回传的json字符串,解析并更新页面组件
* @param jsonstr
*/
void updateUiByjson(String jsonstr);
}
}
读取
package animtest.com.example.e531.listview_requestdata.Utils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
/**
* Created by e531 on 2017/10/12.
*/
public class StreamToString {
* Created by e531 on 2017/10/12.
*/
public class StreamToString {
public static String streamToStr(InputStream inputStream,String chartSet){
StringBuilder builder=new StringBuilder();
try {
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream,chartSet));
String con;
while ((con=br.readLine())!=null){
builder.append(con);
}
try {
BufferedReader br=new BufferedReader(new InputStreamReader(inputStream,chartSet));
String con;
while ((con=br.readLine())!=null){
builder.append(con);
}
br.close();
return builder.toString();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
设置图片的缓存
package animtest.com.example.e531.listview_requestdata;
import android.app.Application;
import android.content.res.Configuration;
import android.os.Environment;
import android.content.res.Configuration;
import android.os.Environment;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.io.File;
/**
* Created by e531 on 2017/10/12.
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
* Created by e531 on 2017/10/12.
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
File cachefile=getExternalCacheDir();
ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this)
.memoryCacheExtraOptions(480, 800)//缓存图片最大的长和宽
.threadPoolSize(2)//线程池的数量
.threadPriority(4)
.memoryCacheSize(2*1024*1024)//设置内存缓存区大小
.diskCacheSize(20*1024*1024)//设置sd卡缓存区大小
.diskCache(new UnlimitedDiscCache(cachefile))//自定义缓存目录
.writeDebugLogs()//打印日志内容
.diskCacheFileNameGenerator(new Md5FileNameGenerator())//给缓存的文件名进行md5加密处理
.build();
.memoryCacheExtraOptions(480, 800)//缓存图片最大的长和宽
.threadPoolSize(2)//线程池的数量
.threadPriority(4)
.memoryCacheSize(2*1024*1024)//设置内存缓存区大小
.diskCacheSize(20*1024*1024)//设置sd卡缓存区大小
.diskCache(new UnlimitedDiscCache(cachefile))//自定义缓存目录
.writeDebugLogs()//打印日志内容
.diskCacheFileNameGenerator(new Md5FileNameGenerator())//给缓存的文件名进行md5加密处理
.build();
ImageLoader.getInstance().init(configuration);
}
}
阅读全文
0 0
- 多条目加载解析方法,读取和图片缓存
- listview的多条目加载和解析
- ImageLoder缓存图片及ListView多条目加载
- listview多条目+图片缓存
- xListView多条目加载,判断有无图片及图片缓存到sd卡
- 多条目加载并用imageLoder加载图片
- 多条目加载有无图片 Adapter
- Xlistview 多条目 图片错位 使用图片缓存显示图片
- Android学习之路-------复习一下缓存和读取缓存,网络加载图片
- ListView多条目加载和ImageLoader
- Android 图片加载及缓存技术解析
- vb picturebox 加载网络图片的两种方法,分无缓存加载和有缓存加载
- Volley进行图片加载和图片缓存
- Pull解析和ListView多条目展示
- RecyclerView条目加载图片实现LruCache三级缓存策略,有效避免 OOM , 提升系统流畅性
- ImageLoader多张图片&listview多条目加载
- 实现根据图片数量不同的多条目加载
- ListView多条目加载
- 梦游ZFA仙境,遥想当年苦读
- java 大整数
- java NIO详解(2)--Buffer
- Eclipse快捷键 10个最有用的快捷键
- 【LibUIDK界面库系列文章】空闲消息
- 多条目加载解析方法,读取和图片缓存
- 桶排序
- 绘制各种图形
- PKU 2774 Long Long Message 后缀数组 最长公共子串
- 关于失业(一)
- 九度OJ——1009二叉搜索树
- [iOS]Mac系统下安装windows共享打印机采坑记
- mybatis逆向工程
- 哥德巴赫猜想