一个简单的网络封装工具类和Image Loader

1>HttpUtils的HttpURLConnection
public class HttpUtils {
    private static HttpUtils httpUtils;
    private HttpListener httpListnenr;
    public static HttpUtils getInstance(){
        if (httpUtils==null){
            httpUtils = new HttpUtils();
        }
        return httpUtils;
    }
public void getdata(String url){
        MyAsycTask asycTask = new MyAsycTask();
        asycTask.execute(url);
}
class MyAsycTask extends AsyncTask<String,Void,String>{

    @Override
    protected String doInBackground(String... strings) {
        String json = "";
        try {
            URL url = new URL(strings[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            if (connection.getResponseCode()==200){
                InputStream inputStream = connection.getInputStream();
                json = StreamToString(inputStream,"UTF-8");

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
private String StreamToString(InputStream inputStream,String s){
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    BufferedReader reader  = new BufferedReader(inputStreamReader);
    String s1 = null;
    StringBuilder builder = new StringBuilder();
    try {
        while ((s1=reader.readLine())!=null){
            builder.append(s1);
        }
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return builder.toString();
}
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        httpListnenr.getjsondata(s);
    }
}
    //定义接口
    public interface HttpListener{
        void getjsondata(String json);
    }
    public void setHttpListnenr(HttpListener httpListnenr){
        this.httpListnenr=httpListnenr;
    }
}

2>MyApp的ImageLoader

public class Myapp extends Application {
    private Context context;
    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        imageLoader();
    }
    private void imageLoader() {
        String path = Environment.getExternalStorageDirectory()+"Image";
        File cache = new File(path);
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .memoryCacheExtraOptions(480,800)
                .diskCacheExtraOptions(480,800,null)
                .threadPoolSize(3)
                .threadPriority(Thread.NORM_PRIORITY-2)
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                .memoryCacheSize(2 * 1024 * 1024)
                .memoryCacheSizePercentage(13)
                .diskCache(new UnlimitedDiskCache(cache))
                .diskCacheSize(50 * 1024 * 1024)
                .diskCacheFileCount(100)
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .writeDebugLogs()
                .build();
        ImageLoader.getInstance().init(config);
    }
    //图片处理
    public  static DisplayImageOptions getOptions(){
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageForEmptyUri(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher)
                .resetViewBeforeLoading(false)
                .delayBeforeLoading(0)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .displayer(new RoundedBitmapDisplayer(300))
                .handler(new Handler())
                .build();
        return options;
    }
}

猜你喜欢

转载自blog.csdn.net/SuperZhongyulong/article/details/81276590