雖然現在用了都是三方工具,很方便,不需要處理異步。但是最基礎的也不能忘卻。
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);
}
}