2012-07-06 2 views
1

http를 통해 오디오 파일을 얻으려고 시도하는 중입니다. 안정된 서비스에서 얻은 텍스트 XML 서비스를 받았지만 오디오 파일을 어떻게 처리하는지 혼란 스럽습니다.HTTP를 통해 오디오 파일을 가져 오는 방법은 무엇입니까?

코드는 XML 응답과 보안 편안한 서비스를 호출하는

String callWebService(String serviceURL) { 
     // http get client 
     HttpClient client = getClient(); 
     HttpGet getRequest = new HttpGet(); 

     try { 
      // construct a URI object 
      getRequest.setURI(new URI(serviceURL)); 
     } catch (URISyntaxException e) { 
      Log.e("URISyntaxException", e.toString()); 
     } 

     // buffer reader to read the response 
     BufferedReader in = null; 
     // the service response 
     HttpResponse response = null; 
     try { 
      // execute the request 
      response = client.execute(getRequest); 
     } catch (ClientProtocolException e) { 
      Log.e("ClientProtocolException", e.toString()); 
     } catch (IOException e) { 
      Log.e("IO exception", e.toString()); 
     } 
     try { 
      in = new BufferedReader(new InputStreamReader(response.getEntity() 
        .getContent())); 
     } catch (IllegalStateException e) { 
      Log.e("IllegalStateException", e.toString()); 
     } catch (IOException e) { 
      Log.e("IO exception", e.toString()); 
     } 
     StringBuffer buff = new StringBuffer(""); 
     String line = ""; 
     try { 
      while ((line = in.readLine()) != null) { 
       buff.append(line); 
      } 
     } catch (IOException e) { 
      Log.e("IO exception", e.toString()); 
      return e.getMessage(); 
     } 

     try { 
      in.close(); 
     } catch (IOException e) { 
      Log.e("IO exception", e.toString()); 
     } 
     // response, need to be parsed 
     return buff.toString(); 
    } 

답변

0

수도 있습니다 하나의 도움이 당신에게 ..

public static void downloadFile(String fileURL, String fileName) { 
    try { 
     // fileURL=fileURL.replaceAll("amp;", ""); 
     Log.e(fileURL, fileName); 
     String RootDir = Environment.getExternalStorageDirectory() 
       .toString(); 

     File RootFile = new File(RootDir); 
     new File(RootDir + Commons.dataPath).mkdirs(); 
     File file = new File(RootFile + Commons.dataPath + fileName); 
     if (file.exists()) { 

      file.delete(); 
     } 
     file.createNewFile(); 

     URL u = new URL(fileURL); 
     HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
     c.setRequestMethod("GET"); 
     c.setDoOutput(true); 
     c.connect(); 
     FileOutputStream f = new FileOutputStream(new File(
       "mnt/sdcard"+Commons.dataPath + fileName)); 
     InputStream in = c.getInputStream(); 
     byte[] buffer = new byte[1024]; 
     int len1 = 0; 

     while ((len1 = in.read(buffer)) > 0) { 
      f.write(buffer, 0, len1); 
     } 
     f.close(); 

    } catch (Exception e) { 

     e.printStackTrace(); 
    } 

} 
관련 문제