WebTool 网页信息获取,可在主线程中调用

48 篇文章 0 订阅
1 篇文章 0 订阅

WebTool.java


package sci.tool;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

import org.json.JSONObject;

import sci.tool.ThreadTool.ThreadPram;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;


/** WebTool.java: 网页信息获取,可在主线程中调用
 * 
 * 1、byte[] 	GetBytes(final String url)
 * 2、String 	GetString(String dataUrl)
 * 3、JSONObject 	GetJSONObject(String jsonUrl)
 * 4、Bitmap 	GetBitmap(String imgUrl)
 * 5、Drawable 	GetDrawable(String imgUrl)
 * 
 * ----- 2018-6-7 上午11:00:03 scimence */
public class WebTool
{
	// 缓存Drawable图像
	private static HashMap<String, Drawable> DrawableDic = new HashMap<String, Drawable>();
	
	/** 从网络上下载图片,转为Drawable */
	public static Drawable GetDrawable(String imgUrl)
	{
		Drawable drawable = null;
		
		if (DrawableDic.containsKey(imgUrl))
			drawable = DrawableDic.get(imgUrl);	// 从缓存读取图像
		else
		{
			Bitmap bmp = GetBitmap(imgUrl);						// 从服务器端下载图像
			if (bmp != null) drawable = Bitmap2Drawable(bmp);			// 转化为Drawable
			if (drawable != null) DrawableDic.put(imgUrl, drawable);		// 记录图像
		}
		
		return drawable;
	}
	
	/** 从网络上下载图片资源 */
	public static Bitmap GetBitmap(String imgUrl)
	{
		Bitmap bmp = null;
		try
		{
			byte[] data = GetBytes(imgUrl);								// 下载数据
			bmp = BitmapFactory.decodeByteArray(data, 0, data.length);	// 载入Bitmap
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return bmp;
	}
	
	/** Bitmap转化为Drawable */
	public static Drawable Bitmap2Drawable(Bitmap bitmap)
	{
		BitmapDrawable drawable = new BitmapDrawable(bitmap);
		return drawable;
	}
	
	/** Drawable转化为Bitmap */
	public static Bitmap Drawable2Bitmap(Drawable drawable)
	{
		BitmapDrawable bitDrawable = (BitmapDrawable) drawable;
		return bitDrawable.getBitmap();
	}
	
	
	/** 获取指定网址的数据 */
	public static String GetString(String dataUrl)
	{
		String Str = "";
		try
		{
			byte[] data = GetBytes(dataUrl);	// 下载数据
			Str = new String(data);				// 转化为字符串
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return Str;
	}

	/** 获取指定网址的数据为JSON */
	public static JSONObject GetJSONObject(String jsonUrl)
	{
		String webData = WebTool.GetString(jsonUrl);
		JSONObject webJson = null;
		try
		{
			webJson = new JSONObject(webData);
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		return webJson;
	}
	
	
	
	// ------------------------
	// 网络数据载入
	// ------------------------
	
	static HashMap<Long, byte[]> GetBytesDic = new HashMap<Long, byte[]>();
	
	/** 获取指定网址的数据,函数可在任意线程中执行,包括主线程 */
	public static byte[] GetBytes(final String url)
	{
		final long KEY = System.currentTimeMillis();
		if (!GetBytesDic.containsKey(KEY)) GetBytesDic.put(KEY, null);
		
		// 在非主线程中执行网络请求,获取数据
		ThreadTool.RunInCachedThread(new ThreadPram()
		{
			@Override
			public void Function()
			{
				byte[] data = GetBytes_process(url);
				GetBytesDic.put(KEY, data);
			}
		});
		
		// 等待异步线程中的网络请求逻辑执行完成
		while (GetBytesDic.get(KEY) == null) 	// 未获取到数据则
		{
			if (System.currentTimeMillis() > KEY + 1000 * 3) break;	// 超出3秒则终止
			Sleep(50); // 延时等待异步线程逻辑执行完成
		}
		
		byte[] data = GetBytesDic.get(KEY);
		GetBytesDic.remove(KEY);
		
		return data;
	}
	
	/** 获取指定网址的数据 */
	public static byte[] GetBytes_process(String url)
	{
		byte[] data = new byte[0];
		try
		{
			URL webUrl = new URL(url);
			URLConnection con = webUrl.openConnection();	// 打开连接
			InputStream in = con.getInputStream();			// 获取InputStream
			
			data = InputStreamToByte(in);					// 读取输入流数据
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return data;
	}

	/** InputStream -> Byte */
	public static final byte[] InputStreamToByte(InputStream in)
	{
		byte[] bytes = {};
		
		try
		{
			ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
			byte[] data = new byte[1024];
			int count = 0;
			while ((count = in.read(data, 0, 1024)) > 0)
			{
				byteOutStream.write(data, 0, count);
			}
			
			bytes = byteOutStream.toByteArray();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		return bytes;
	}
	
	/** 当前线程延时毫秒 */
	private static void Sleep(long timeMillion)
	{
		try
		{
			Thread.sleep(timeMillion);
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
	}
	
}

ThreadTool.java 线程辅助操作类

package sci.tool;

import java.util.concurrent.Executors;
import android.os.Handler;
import android.os.Looper;


/** 线程辅助处理类,用于在主线程和其他线程中执行逻辑 */
public class ThreadTool
{
	// 调用示例
	public static void Example()
	{
		ThreadTool.RunInMainThread(new ThreadPram()
		{
			@Override
			public void Function()
			{
				// TODO Auto-generated method stub
				// 在主线程执行逻辑
			}
		});
	}
	
	// ---------------------------------------------
	
	/** 线程辅助处理类对象参数 */
	public static abstract class ThreadPram
	{
		/** 需要在线程中执行的逻辑 */
		public abstract void Function();
	}
	
	/** 在主线程执行Function —— UI界面相关控件逻辑需在主线程中执行 */
	public static void RunInMainThread(final ThreadPram param)
	{
		getMainHandler().post(new Runnable()
		{
			@Override
			public void run()
			{
				param.Function();
			}
		});
	}
	
	/** 在主线程中延时delayMillis毫秒,执行Function —— UI界面相关控件逻辑需在主线程中执行 */
	public static void RunInMainThread(final ThreadPram param, long delayMillis)
	{
		getMainHandler().postDelayed(new Runnable()
		{
			@Override
			public void run()
			{
				param.Function();
			}
		}, delayMillis);
	}
	
	/** 在其他线程执行Function —— 网络请求需在主线程之外的其他线程执行 */
	public static void RunInCachedThread(final ThreadPram param)
	{
		Executors.newCachedThreadPool().execute(new Runnable()
		{
			@Override
			public void run()
			{
				param.Function();
			}
		});
	}
	
	/** 当前线程是否为主线程 */
	public static boolean isUiThread()
	{
		return Thread.currentThread() == Looper.getMainLooper().getThread();
	}
	
	/** 获取主线程Handler */
	public static Handler getMainHandler()
	{
		return new Handler(Looper.getMainLooper());
	}
	
	/** 获取当前线程Handler */
	public static Handler getCurrentHandler()
	{
		return new Handler(Looper.myLooper());
	}
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值