2011-08-24 2 views
0

내 응용 프로그램에서 MP3 파일, 비디오 파일 등과 같은 서버에서 데이터를 가져 오려고합니다. 응용 프로그램은 서버에서받은 비디오 파일 목록을 표시해야합니다.서버에서 데이터를받는 방법?

어떻게하면됩니까?

답변

0
/** this function will download content from the internet */ 
    static int writeData(String fileurl, boolean append, String path, 
      String filename, Activity mContext) throws CustomException { 

     URL myfileurl = null; 
     ByteArrayBuffer baf = null; 
     HttpURLConnection conn = null; 
     String mimeType=""; 
     final int length; 
     try { 
      myfileurl = new URL(fileurl); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } 
     try { 
      conn = (HttpURLConnection) myfileurl 
        .openConnection(); 
      conn.setDoInput(true); 
      conn.connect(); 
      conn.setConnectTimeout(100000); 

      length = conn.getContentLength(); 
      mimeType=conn.getContentType().toString(); 
      System.out.println("Extension..."+mimeType); 
      if(mimeType.equalsIgnoreCase("application/vnd.adobe.adept+xml") || mimeType.equalsIgnoreCase("text/html; charset=utf-8")) 
       return 0; 

      if (length > 0) { 
       InputStream is = conn.getInputStream(); 
       BufferedInputStream bis = new BufferedInputStream(is); 
       baf = new ByteArrayBuffer(1000); 
       int current = 0; 
       while ((current = bis.read()) != -1) { 
        try { 
         baf.append((byte) current); 

         mBufferError=false; 
        } catch (Exception e){ 
         // TODO: handle exception 
         mBufferError=true; 
         e.printStackTrace(); 
         throw new CustomException("@@@ memory problem ", "Buffer Error"); 
        } 
       } 
      } 

     } catch (IOException e) { 
      mBufferError=true; 
      e.printStackTrace(); 
     } 
     try{ 
     if(conn.getResponseCode()==200 && mBufferError==false) 
     { 
      path = path + "/" + filename; 
      boolean appendData = append; 
      FileOutputStream foutstream; 

       File file = new File(path); 
      boolean exist = false; 

      try { 
       if (appendData) 
        exist = file.exists(); 
       else 
        exist = file.createNewFile(); 
      } catch (IOException e) { 
       try { 
        return 1; 
       } catch (Exception err) { 
        Log.e("SAX", err.toString()); 
       } 
      } 
      if (!appendData && !exist) { 
      } else if (appendData && !exist) { 

      } else { 
       try { 
        foutstream = new FileOutputStream(file, appendData); 
        foutstream.write(baf.toByteArray()); 
        foutstream.close(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
     }catch (Exception e) { 
      // TODO: handle exception 
      throw new CustomException("@@@ I/O problem ", "I/O Error"); 
     } 
     return 1; 
    } 

도움이되기를 바랍니다 비디오 에 대한 전체 검색을 확장 (하고 .3gp)를 사용하여 파일을 다운로드
0

확인이 링크,

https://stackoverflow.com/search?q=how+to+download+mp3+%2Cvideos+from+server+in+android

이 코드를

url = "your url name+filename.jpg,mp3,etc..." 
    FileName = "/sdcard/savefilename" // save in your sdcard 


try{ 
      java.io.BufferedInputStream in = new java.io.BufferedInputStream(new  java.net.URL(url).openStream()); 
     java.io.FileOutputStream fos = new java.io.FileOutputStream(FileName); 
     java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024); 
     byte[] data = new byte[1024]; 
     int x=0; 
     while((x=in.read(data,0,1024))>=0){ 
      bout.write(data,0,x);    
     } 
     fos.flush(); 
     bout.flush(); 
     fos.close(); 
     bout.close(); 
     in.close(); 
    } 
    catch (Exception ex) 
    { 
    } 

을 시도하고 당신이 MediaPlayer를 을 사용하고 활동 에 MediaPlayer를의 객체를 생성하고 재생하려는 후. mp.reset(); mp.start(); 한 번

관련 문제