Android 联网获取普通数据 和 JSON 数据工具类

package com.example.viewpager_homework_tools;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
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){
		boolean bl=false;
		ConnectivityManager manager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info=manager.getActiveNetworkInfo();
		if(info!=null){
			return info.isConnected();
		}
		return bl;
	}
	public static String getJsonContent(String url){
		String result = "";
		URL url2;
		InputStream is;
		InputStreamReader isr;
		BufferedReader br;
		try {
			url2 = new URL(url);
			HttpURLConnection connection = 
					(HttpURLConnection)url2.openConnection();
			is = connection.getInputStream();
			isr = new InputStreamReader(is,"utf-8");
			String line = "";
			br = new BufferedReader(isr);
			while ((line=br.readLine())!=null) {
				   result+=line;
			}
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static String getJsonContent2(String url){
		String result = "";
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		HttpResponse response = null;
		try {
			response = httpClient.execute(httpGet);
			if (response.getStatusLine().getStatusCode()==200) {
				result = EntityUtils.toString(response.getEntity(), "utf-8");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public static byte[] getByteContent2(String url){
		byte []data = null;
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		HttpResponse response = null;
		try {
			response = httpClient.execute(httpGet);
			if (response.getStatusLine().getStatusCode()==200) {
				data = EntityUtils.toByteArray(response.getEntity());
			}
			return data;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		return data;
		
	}
	
	public static byte[] getByteContent(String url){
		try {
			URL url2 = new URL(url);
			HttpURLConnection httpConn = (HttpURLConnection) url2
							.openConnection();
			httpConn.setDoInput(true);    //允许输入内容
			httpConn.setDoOutput(false);
			httpConn.connect();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			if (httpConn.getResponseCode()==200) {
				BufferedInputStream bis = new BufferedInputStream(httpConn.getInputStream());
				byte[] buffer = new byte[8*1024];
				int c = 0;
				while ((c=bis.read(buffer))!=-1) {
					baos.write(buffer, 0, c);
					baos.flush();
				}
				byte[] data = baos.toByteArray();
				return data;
			}	
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值