[置顶] 网络请求以及网络请求下载图片的工具类 android开发java工具类

package cc.jiusan.www.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class HttpUtils {
	/**
	 * 判断当前网络是否连接
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isNetWorkConn(Context context) {
		ConnectivityManager manager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = manager.getActiveNetworkInfo();
		if (info != null) {
			return info.isConnected();
		}
		return false;
	}

	// 网络请求下载图片
	public static byte[] loadImageFromNet(String url) {
		//OKHttp
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet getRequest = new HttpGet(url);
			HttpResponse response = client.execute(getRequest);
			// 500 服务器 错误    
			if (response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				return EntityUtils.toByteArray(entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
}

你可能感兴趣的:(java,android,Android开发,操作系统,androidstudio)