虽然现在用了都是三方工具,很方便,不需要处理异步。但是最基础的也不能忘却。
public class HttpUtils {
/**
* get请求方式
*
**/
public static String getString(String webSite) {
BufferedReader bReader = null;
InputStream isInputStream = null;
URL url;
StringBuilder sBuilder = new StringBuilder();
try {
url = new URL(webSite);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.setDoOutput(false);
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(5000);
if (connection.getResponseCode() == 200) {
isInputStream = connection.getInputStream();
bReader = new BufferedReader(new InputStreamReader(
isInputStream, "utf-8"));
String line = "";
while ((line = bReader.readLine()) != null) {
sBuilder.append(line);
}
return sBuilder.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bReader != null) {
bReader.close();
}
if (sBuilder != null) {
isInputStream.close();
}
} catch (Exception e2) {
}
}
return null;
}
/**
* get请求方式
* 返回字节数组
**/
public static byte[] getString(String path){
HttpURLConnection conn = null;
try {
URL url = new URL(path);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if(code==200){
InputStream is = conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len=is.read(buf))!=-1){
out.write(buf,0,len);
}
return out.toByteArray();
}else{
throw new RuntimeException("网络访问异常:" + code);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(conn!=null){
conn.disconnect();
conn = null;
}
}
return null;
}
/**
* post请求方式,还需要异步中传来隐形参数map集合共同作用
*
**/
public static String getPost(String url,Map<String,String> data){
try {
URL url2 = new URL(url);
HttpURLConnection conn = (HttpURLConnection) url2.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(8000);
conn.setReadTimeout(5000);
StringBuilder sb = new StringBuilder();
Set<Map.Entry<String,String>> set = data.entrySet();
Iterator<Map.Entry<String,String>> it = set.iterator();
while (it.hasNext()) {
Map.Entry<String,String> ma = it.next();
String key = ma.getKey();
sb.append(key+"=");
String value = ma.getValue();
sb.append(value+"&");
}
PrintWriter pw = new PrintWriter(conn.getOutputStream());
pw.write(sb.toString());
pw.flush();
int code = conn.getResponseCode();
if(code==200){
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
String line = "";
StringBuilder sb2 = new StringBuilder();
while((line=br.readLine())!=null){
sb2.append(line);
}
return sb2.toString();
}else{
throw new RuntimeException("下载异常");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 下载图片 可以返回字节型,也可以返回bitmap型,后面是图片压缩
*
**/
public static Bitmap getPicture(String webSite) {
ByteArrayOutputStream baos = null;
InputStream is = null;
byte[] data;
URL url;
try {
url = new URL(webSite);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置参数
conn.setDoInput(true); // 可以读取内容(服务端提供)
conn.setDoOutput(false); // 写入内容(写入到服务端)
conn.setRequestMethod("GET");
conn.setConnectTimeout(8000); // 连接超时时间
conn.setReadTimeout(5000); // 读取超时时间
if (conn.getResponseCode() == 200) { // 成功状态
is = conn.getInputStream();
byte[] buffer = new byte[8*1024];
int len = 0;
baos = new ByteArrayOutputStream();
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len); //读取所有内容
}
data = baos.toByteArray(); //转换成byte[]
return getBmFromByte(data);
} else {
// 查找HTTP Code编码,解决问题
Log.e("123", "网络下载错误编码(byte[])-->" + conn.getResponseCode());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null)
baos.close();
if (is != null)
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 下载图片 直接返回bitmap型
*
**/
public static Bitmap getString(String path){
HttpURLConnection conn = null;
try {
URL url = new URL(path);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if(code == 200) {
InputStream is = conn.getInputStream();
// 转换成图片
Bitmap bm = BitmapFactory.decodeStream(is);
// iv.setImageBitmap(bm);
SystemClock.sleep(2000);
return bm;
} else {
throw new RuntimeException("网络访问异常:" + code);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(conn != null) {
conn.disconnect();
conn = null;
}
}
return null;
}
/**
* 可以使用图片压缩,也可以不使用,一般情况下,图片原始大小和实际需要大小一致时,压缩会显示的不清楚,
* 所以一般是当图片很大时(也就是说压缩后的图片大小比你实际需要的布局还要大或者差不多),才使用这种方法
**/
public static Bitmap getBmFromByte(byte[] data){
// BitmapFactory.Options options = new BitmapFactory.Options();
// options.inJustDecodeBounds = true;
// Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length, options);
// int orgHeight = options.outHeight;
// int orgWidth = options.outWidth;
// options.inSampleSize = 4;
// options.inJustDecodeBounds = false;
// return BitmapFactory.decodeByteArray(data, 0, data.length, options);
//这是不采取压缩的方法 直接转型
return BitmapFactory.decodeByteArray(data, 0, data.length);
}
}