public byte[] requestsByteArr(String url, HashMap<String, String> head) { byte[] result = null; try { HttpClient client = newInstance(); HttpGet request = new HttpGet(); if (head != null) { for (Entry<String, String> entry : head.entrySet()) { Tools.log("TAG", "" + entry.getKey() + " - " + entry.getValue()); request.addHeader(entry.getKey(), entry.getValue()); } } request.setURI(new URI(url)); HttpResponse response = client.execute(request); result = readStream(response.getEntity().getContent()); } catch (Exception e) { e.printStackTrace(); } finally { } return result; }
@Override public String requests(String url, HashMap<String, String> head) { String result = null; BufferedReader reader = null; try { HttpClient client = newInstance(); HttpGet request = new HttpGet(); if (head != null) { for (Entry<String, String> entry : head.entrySet()) { Tools.log("TAG", "" + entry.getKey() + " - " + entry.getValue()); request.addHeader(entry.getKey(), entry.getValue()); } } request.setURI(new URI(url)); HttpResponse response = client.execute(request); reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer strBuffer = new StringBuffer(""); String line = null; while ((line = reader.readLine()) != null) { strBuffer.append(line); } result = strBuffer.toString(); } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); reader = null; } catch (IOException e) { e.printStackTrace(); } } } return result; }
/* * 得到图片字节流 数组大小 */ public static byte[] readStream(InputStream inStream) { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); try { byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } outStream.close(); inStream.close(); } catch (Exception e) { } Tools.log("TAG", "outStream.toByteArray()=" + outStream.toByteArray()); return outStream.toByteArray(); }