2013-07-03 14 views
5

Opracowuję aplikację na Androida, która musi pobrać niektóre pliki z usługi internetowej.Android Pobierz plik pdf w wyniku httppost

bramki

chcę pobrać plik PDF z usługi internetowej i pokazać go na telefon.

Problem

jestem nowicjuszem pracy z serwisów internetowych i I'dont mieć tak jasne, jak pobrać plik i otworzyć go.

Tu jest mój kod do tej pory:

public void downloadFile(String username, String password, String filename) {  
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    nameValuePairs.add(new BasicNameValuePair("username", username)); 
    nameValuePairs.add(new BasicNameValuePair("password", password)); 
    nameValuePairs.add(new BasicNameValuePair("filename", filename)); 

    String output = httpPost(URL_DESCARGAS, nameValuePairs); 
} 

private String httpPost(String url, List<NameValuePair> nameValuePairs) { 
    /* Create new HttpClient and Post Header */ 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(url); 

    try { 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); 

     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 

     /* Get output */    
     return inputStreamToString(response.getEntity().getContent()).toString(); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return ""; 
} 

nie mam wrócić ciąg reakcji, ale nie wiem, co mam robić. Jak wyświetlić plik PDF, który zwraca usługę sieciową?

W innym działaniu muszę wykonać to samo, ale usługa sieciowa zwraca .png i chcę to pokazać w widoku obrazu lub coś podobnego. Robi się to w ten sam sposób?

Dziękuję bardzo. Długo szukałem, ale niczego nie znalazłem.

Edit

Znalazłem ten link: http://www.androidsnippets.com/download-an-http-file-to-sdcard-with-progress-notification i używam to, co mówi, ale plik nie jest tworzony (nie widzę go w katalogu głównym sdcard).

Oto mój zmodyfikowany kod:

public void downloadFile(String username, String password, String filename) {  
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    nameValuePairs.add(new BasicNameValuePair("username", username)); 
    nameValuePairs.add(new BasicNameValuePair("password", password)); 
    nameValuePairs.add(new BasicNameValuePair("filename", filename)); 

    HttpResponse response = httpPost(URL_DESCARGAS, nameValuePairs); 
    HttpEntity entity = response.getEntity(); 
    if (entity != null) { 
     try { 
      InputStream inputStream = entity.getContent(); 
      //set the path where we want to save the file 
      //in this case, going to save it on the root directory of the 
      //sd card. 
      File SDCardRoot = Environment.getExternalStorageDirectory(); 
      //create a new file, specifying the path, and the filename 
      //which we want to save the file as. 
      File file = new File(SDCardRoot, filename); 

      //this will be used to write the downloaded data into the file we created 
      FileOutputStream fileOutput = new FileOutputStream(file); 

      //create a buffer... 
      byte[] buffer = new byte[1024]; 
      int bufferLength = 0; //used to store a temporary size of the buffer 

      //now, read through the input buffer and write the contents to the file 
      while ((bufferLength = inputStream.read(buffer)) > 0) { 
       //add the data in the buffer to the file in the file output stream (the file on the sd card 
       fileOutput.write(buffer, 0, bufferLength); 
      } 
      //close the output stream when done 
      fileOutput.close(); 
     } catch (IllegalStateException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

Czy możesz mi pomóc? Muszę utworzyć ten plik i pokazać go w telefonie w tej chwili.

Odpowiedz

3

Rozwiązałem to. Nie pamiętam, aby umieścić uprawnienia w AndroidManifest.xml.

Tu jest mój kod, który pobiera moc z HttpPost tworzy plik na karcie SD i otwiera je w przeglądarce PDF, które użytkownik ma zainstalowaną w urządzeniu:

klasy Activity

public void downloadInvoice(View view) { 
    boolean ok = myApplication.downloadFile("pdf"); 
    if (!ok) { 
     //If not ok, show error message 
    } else { 
     //Open file in pdf viewer that user has in the device 
     File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() 
       +"/"+ myApplication.getActualUser().getActualInvoice().getPdf()); 
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file)); 
     intent.setType("application/pdf"); 
     startActivity(intent); 
    } 
} 

AndroidManifest .xml

... 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
...