2013-07-05 5 views
0

나는 안드로이드 프로그래밍의 초보자입니다.Android에서 다운로드

나는 안드로이드

로 파일을 다운로드하는 몇 가지 문제가있어 내가 사용 Httpost, Httpget 및 hhtpurlconnection 먼저이 모든 에서 작동하지 않습니다 그리고 세 번째는 다운로드 할 수 없습니다 견인 시간

XML을 파싱하기 위해 다른 xml을 string 또는 inputstream (또는 이들로 변환 가능한 무언가)에 다운로드하는 방법을 원합니다. 방법 외에 같은 것을 할 수 있어야한다 :

conn.setRequestProperty("Authorization", "Basic " + encodedStr); 

XMLS 당신이 파일을 다운로드하는 데 사용할 수있는 예를 들어 아래는 API

+0

는이 질문에 대답을 참조하십시오 http://stackoverflow.com/ 질문/13481027/i-want-in-download-xml-file-for-parsing – Chucky

+0

그러나 두 번째로 나는 그것을 사용하여 오류를 표시하고 종료 –

+0

@EbrahimTahernejad 그런 다음 오류 – keyser

답변

0

의 응답 때문이다. 자연스럽게 올바른 URL을 사용해야합니다.

public InputStream downloadXmlFileStreamUsingUrl(final String url) { 

    log.info(String.format("downloadXmlFileStreamUsingUrl: %s", url)); 


    final HttpGet getRequest = new HttpGet(url); 
    HttpClient client; 
    try { 
     client = new DefaultHttpClient(); 

     final HttpResponse getResponse = client.execute(getRequest); 
     final int statusCode = getResponse.getStatusLine().getStatusCode(); 

     if (statusCode != HttpStatus.SC_OK) { 
     log.warn("Error " + statusCode + " for URL " + url); 
     return null; 
     } 

     final HttpEntity getResponseEntity = getResponse.getEntity(); 
     final InputStream content = getResponseEntity.getContent(); 
     return content; 

    } catch (final IOException e) { 
     getRequest.abort(); 
     log.warn("Exception in downloadXmlFileStreamUsingUrl, error for URL " + url + e, e); 
    } 
    finally { 
     // When HttpClient instance is no longer needed, 
     // shut down the connection manager to ensure 
     // immediate deallocation of all system resources 
     client.getConnectionManager().shutdown(); 
    } 

    return null; 

    } 
+0

줄 10 문제 : | –

+0

사실, 그것을 잊어 버렸습니다. 코드를 편집했습니다. 지금 당장해야합니다. –

2

여기 서버에서 이미지 파일을 다운로드하는 방법을 보여줍니다. 내가 사진 폴더가 로컬 서버에 그 asuming하고 당신이 그에서 그림을 다운로드 .. 를 사용하여 다음과 같은 코드가 당신을 도울 수 있습니다 ..

public class DownloadType1 extends Activity{ 

    String dwnload_file_path = "http://10.0.2.2/pictures/webicon.PNG"; 
    String dest_file_path = Environment.getRootDirectory()+"/pictures/img1.png"; 
    ProgressDialog dialog = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.download1); 
    } 


    public void onClick(View v) { 

     dialog = ProgressDialog.show(DownloadType1.this, "", "Downloading file...", true); 
      new Thread(new Runnable() { 
       public void run() { 
         downloadFile(dwnload_file_path, dest_file_path); 
       } 
       }).start();    
    } 

    public void downloadFile(String url, String dest_file_path) { 
     try { 
      File dest_file = new File(dest_file_path); 
      URL u = new URL(url); 
      URLConnection conn = u.openConnection(); 
      int contentLength = conn.getContentLength(); 

      DataInputStream stream = new DataInputStream(u.openStream()); 

      byte[] buffer = new byte[contentLength]; 
      stream.readFully(buffer); 
      stream.close(); 

      DataOutputStream fos = new DataOutputStream(new FileOutputStream(dest_file)); 
      fos.write(buffer); 
      fos.flush(); 
      fos.close(); 
      hideProgressIndicator(); 

     } catch(FileNotFoundException e) { 
      hideProgressIndicator(); 
      return; 
     } catch (IOException e) { 
      hideProgressIndicator(); 
      return; 
     } 
    } 

    void hideProgressIndicator(){ 
     runOnUiThread(new Runnable() { 
      public void run() { 
       dialog.dismiss(); 
      } 
     }); 
    } 
} 
+0

고마워요 : D 이것은 제가 원한 것이 아니지만 제 프로젝트의 또 다른 부분입니다 : D –

관련 문제