http请求后获得所需要的是字符串的时候
URL url=new URL(strurl); try { HttpURLConnection conn=(HttpURLConnection) url.openConnection(); conn.setConnectTimeout(10*1000); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.connect(); int size=conn.getContentLength(); InputStream input=conn.getInputStream(); InputStreamReader inputreader=new InputStreamReader(input); BufferedReader bufferreader=new BufferedReader(inputreader); StringBuffer strbuffer=new StringBuffer(); byte [] b=new byte [1024]; String temp; while((temp=bufferreader.readLine())!=null){ strbuffer.append(b); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
文件下载
/** * 文件下载 */ private void downloadFile() { try { URL u = new URL(url); URLConnection conn = u.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); fileSize = conn.getContentLength(); if(fileSize<1||is==null) { sendMessage(DOWNLOAD_ERROR); }else{ sendMessage(DOWNLOAD_PREPARE); FileOutputStream fos = new FileOutputStream(getPath()); byte[] bytes = new byte[1024]; int len = -1; while((len = is.read(bytes))!=-1) { fos.write(bytes, 0, len); downloadSize+=len; sendMessage(DOWNLOAD_WORK); } sendMessage(DOWNLOAD_OK); is.close(); fos.close(); } } catch (Exception e) { sendMessage(DOWNLOAD_ERROR); e.printStackTrace(); } } /** * 得到文件的保存路径 * @return * @throws IOException */ private String getPath() throws IOException { String path = FileUtil.setMkdir(this)+File.separator+url.substring(url.lastIndexOf("/")+1); return path; }
BitMap对象
URL url = new URL(params); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setDoInput(true); conn.connect(); InputStream inputStream=conn.getInputStream(); bitmap = BitmapFactory.decodeStream(inputStream