首页 > 代码库 > 赵雅智_Android_网络操作工具类

赵雅智_Android_网络操作工具类

package com.cards.basic.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

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

import com.cards.commom.Common;

/**
 * @explaination 网络操作工具类
 * @author yazhizhao
 * @time 2014-7-7下午3:35:32
 */
public class HttpUtil {

	private static HttpUtil instance;
	private HttpURLConnection conn;
	private InputStream is;
	private OutputStream os;

	public static HttpUtil getInstance(Context context) {
		if (instance == null) {
			instance = new HttpUtil(context);
		}
		return instance;
	}

	private final static int TIMEOUT = 30 * 1000;

	/**
	 * 日志工具标签
	 */
	private static String tag = "HttpUtil";
	/**
	 * 上下文环境
	 */
	private Context context;

	/**
	 * 构造方法
	 * 
	 * @param context
	 *            上下文环境
	 */
	public HttpUtil(Context context) {
		this.context = context;
	}

	/**
	 * 
	 * @explaination service发送Http请求
	 * @author yazhizhao
	 * @time 2014-7-7下午3:36:36
	 * @param urlpath
	 * @return
	 * @throws Exception
	 */
	public String postAccessServer(String urlpath, String requestJSONStr) {
		Common.log("url = " + urlpath);
		Common.log("requestJSONStr = " + requestJSONStr);
		String result = null;
		URL url = null;
		conn = null;
		os = null;
		try {
			url = new URL(urlpath);
			if (conn == null) {
				conn = (HttpURLConnection) url.openConnection();
			}
			conn.setConnectTimeout(TIMEOUT);
			conn.setReadTimeout(TIMEOUT);
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			conn.setUseCaches(false);
			conn.setRequestProperty("connection", "keep-alive");
			conn.setRequestProperty("Charset", "UTF-8");
			if (requestJSONStr != null) {
				byte[] dataUpdate = requestJSONStr.getBytes("UTF-8");
				os = conn.getOutputStream();
				os.write(dataUpdate);
			}

			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			is = conn.getInputStream();
			byte[] temp = new byte[256];
			int len = 0;
			while ((len = is.read(temp)) != -1) {
				baos.write(temp, 0, len);
			}

			byte[] dataResult = baos.toByteArray();
			Common.log("data Length = " + dataResult.length);
			if (dataResult != null || !"".equals(dataResult)) {
				result = new String(dataResult, "UTF-8");
			}
			is.close();
			is = null;
			if (os != null) {
				os.close();
				os = null;
			}
			conn = null;
			url = null;
		} catch (MalformedURLException e) {
			Common.log("MalformedURLException = " + e.toString());
		} catch (IOException e) {
			Common.log("IOException = " + e.toString());
		} catch (Exception e) {
			Common.log("Exception = " + e.toString());
		}
		Common.log("result = " + result);
		Common.log("Stop At HttpUtil");
		return result;
	}

	/**
	 * 
	 * @explaination 判断网络是否可用
	 * @author yazhizhao
	 * @time 2014-7-7下午3:37:20
	 * @param context
	 * @return
	 */
	public static boolean isNetworkAvailable(Context context) {
		boolean flag = false;
		ConnectivityManager localConnectivityManager = (ConnectivityManager) context
				.getApplicationContext().getSystemService(
						Context.CONNECTIVITY_SERVICE);
		if (localConnectivityManager != null) {
			try {
				NetworkInfo localNetworkInfo = localConnectivityManager
						.getActiveNetworkInfo();
				if ((localNetworkInfo == null)
						|| (!localNetworkInfo.isAvailable()))
					flag = false;// 不可用
				else
					flag = true;// 可用
			} catch (Exception e) {
				e.printStackTrace();
				flag = false;
			}
		}
		return flag;
	}
	 
 
 

	/**
	 * 强制断开连接
	 * 
	 * @return
	 */
	public boolean killConnection() {
		try {
			if (is != null) {
				is.close();
				is = null;
			}
			if (os != null) {
				os.close();
				os = null;
			}
			if (conn != null) {
				conn.disconnect();
				conn = null;
			}
		} catch (Exception e) {
			return false;
		}
		return true;
	}
}