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;

}




}
0 0
原创粉丝点击