HttpUtil工具
来源:互联网 发布:17网络用语 编辑:程序博客网 时间:2024/06/07 10:46
package com.bw.aday18listnet.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class HttpUtil {
/**
*判断网络状态
*/
public static boolean isNetConnected(Context mContext){
ConnectivityManager manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if(info !=null && info.isConnectedOrConnecting()){
return true;
}
return false;
}
/**
* 从网络下载Json数据String
* @param path
* @return
*/
public static String getStr(String path,String encoding){
URL url = null;
HttpURLConnection huc = null;
BufferedReader rb = null;
// BufferedInputStream bis = null;
StringBuilder sbR = null;
try {
url = new URL(path);
huc = (HttpURLConnection) url.openConnection();
// huc.setConnectTimeout(5*1000);
huc.setRequestMethod("GET");
huc.connect();
if(huc.getResponseCode()==200){
rb = new BufferedReader(new InputStreamReader(huc.getInputStream()));
// bis = new BufferedInputStream(huc.getInputStream());
sbR = new StringBuilder();
byte[] b = new byte[1024];
String r;
while ((r=rb.readLine())!=null) {
sbR.append(r);
}
}
return sbR.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(rb!=null){
try {
rb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
huc.disconnect();
}
return null;
}
/**
* 从网络上获取byte[]
* @param path
* @return
*/
public static byte[] getByte(String path){
URL url = null;
HttpURLConnection huc = null;
BufferedInputStream bis = null;
ByteArrayOutputStream bos = null;
byte[] byW=null;
try {
url = new URL(path);
huc = (HttpURLConnection) url.openConnection();
// huc.setConnectTimeout(5*1000);
huc.setRequestMethod("GET");
huc.connect();
if(huc.getResponseCode()==200){
bis = new BufferedInputStream(huc.getInputStream());
bos= new ByteArrayOutputStream();
byte[] byR = new byte[1024];
int len;
while ((len=bis.read(byR))!=-1) {
bos.write(byR, 0, len);
bos.flush();
}
byW = bos.toByteArray();
}
return byW;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
huc.disconnect();
}
return null;
}
public static Bitmap getBitmap(String path){
URL url = null;
HttpURLConnection huc = null;
Bitmap bitmap = null;
try {
url = new URL(path);
huc = (HttpURLConnection) url.openConnection();
// huc.setConnectTimeout(5*1000);
huc.setRequestMethod("GET");
huc.connect();
if(huc.getResponseCode()==200){
bitmap = BitmapFactory.decodeStream(huc.getInputStream());
}
return bitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
huc.disconnect();
}
return null;
}
//将byte[]转成bitmap类型
public static Bitmap ByteToBitmap(byte[] b ){
ByteArrayInputStream inputStream = null;
inputStream = new ByteArrayInputStream(b);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class HttpUtil {
/**
*判断网络状态
*/
public static boolean isNetConnected(Context mContext){
ConnectivityManager manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if(info !=null && info.isConnectedOrConnecting()){
return true;
}
return false;
}
/**
* 从网络下载Json数据String
* @param path
* @return
*/
public static String getStr(String path,String encoding){
URL url = null;
HttpURLConnection huc = null;
BufferedReader rb = null;
// BufferedInputStream bis = null;
StringBuilder sbR = null;
try {
url = new URL(path);
huc = (HttpURLConnection) url.openConnection();
// huc.setConnectTimeout(5*1000);
huc.setRequestMethod("GET");
huc.connect();
if(huc.getResponseCode()==200){
rb = new BufferedReader(new InputStreamReader(huc.getInputStream()));
// bis = new BufferedInputStream(huc.getInputStream());
sbR = new StringBuilder();
byte[] b = new byte[1024];
String r;
while ((r=rb.readLine())!=null) {
sbR.append(r);
}
}
return sbR.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(rb!=null){
try {
rb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
huc.disconnect();
}
return null;
}
/**
* 从网络上获取byte[]
* @param path
* @return
*/
public static byte[] getByte(String path){
URL url = null;
HttpURLConnection huc = null;
BufferedInputStream bis = null;
ByteArrayOutputStream bos = null;
byte[] byW=null;
try {
url = new URL(path);
huc = (HttpURLConnection) url.openConnection();
// huc.setConnectTimeout(5*1000);
huc.setRequestMethod("GET");
huc.connect();
if(huc.getResponseCode()==200){
bis = new BufferedInputStream(huc.getInputStream());
bos= new ByteArrayOutputStream();
byte[] byR = new byte[1024];
int len;
while ((len=bis.read(byR))!=-1) {
bos.write(byR, 0, len);
bos.flush();
}
byW = bos.toByteArray();
}
return byW;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
huc.disconnect();
}
return null;
}
public static Bitmap getBitmap(String path){
URL url = null;
HttpURLConnection huc = null;
Bitmap bitmap = null;
try {
url = new URL(path);
huc = (HttpURLConnection) url.openConnection();
// huc.setConnectTimeout(5*1000);
huc.setRequestMethod("GET");
huc.connect();
if(huc.getResponseCode()==200){
bitmap = BitmapFactory.decodeStream(huc.getInputStream());
}
return bitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
huc.disconnect();
}
return null;
}
//将byte[]转成bitmap类型
public static Bitmap ByteToBitmap(byte[] b ){
ByteArrayInputStream inputStream = null;
inputStream = new ByteArrayInputStream(b);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
}
0 0
- HttpUtil工具
- HttpUtil工具类
- httpUtil工具类
- HttpUtil 工具类
- HttpUtil工具类
- HttpUtil工具类
- HttpUtil---网络请求工具类
- Http请求工具类 httputil
- Android工具类系列-HttpUtil
- Http请求工具类HttpUtil
- 网络请求工具类HttpUtil
- HttpUtil
- HttpUtil
- httputil
- HttpUtil
- HttpUtil
- HttpUtil
- HttpUtil
- AngularJS2教程--前序
- iOS内存管理:基本概念与原理
- 制作基本地图-ol3开发
- 滴,学生卡!--Kaggle入门
- URL 传递特殊字符到后台的解决办法
- HttpUtil工具
- PAT 1061 判断题
- Ubuntu源下载
- Numpy 索引
- java 去除首尾空格trim()方法
- 存储过程中的字符串拼接问题
- 循环链表 CircleList
- php 计划任务
- 博为峰Java技术文章 ——JavaSE Swing JLayeredPane面板