【问题标题】:Android Get pdf file as a result of httppostAndroid 通过 httppost 获取 pdf 文件
【发布时间】:2013-06-30 22:54:07
【问题描述】:

我正在开发一个必须从网络服务下载一些文件的 Android 应用程序。

目标

我想从网络服务下载一个 PDF 文件并在手机上显示。

问题

我是使用网络服务的新手,我不太清楚如何获取文件并打开它。

这是我迄今为止的代码:

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 "";
}

我不必返回响应字符串,但我不知道我必须做什么。那么如何显示返回web服务的pdf呢?

在另一个活动中,我必须执行相同的操作,但 Web 服务返回一个 .png,我想在 imageview 或类似的东西中显示它。也是一样的吗?

非常感谢。找了好久也没找到。

编辑

我找到了那个网址:http://www.androidsnippets.com/download-an-http-file-to-sdcard-with-progress-notification 我使用它所说的,但没有创建文件(我在 sdcard 根目录中看不到它)。

这是我修改后的代码:

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();
        }
    }
}

你能帮帮我吗?我现在需要创建该文件并将其显示在手机中。

【问题讨论】:

    标签: android web-services file pdf http-post


    【解决方案1】:

    我已经解决了。我忘了在 AndroidManifest.xml 中添加权限。

    这是我的代码,它获取 httppost 的输出,在 SD 卡中创建文件并在用户安装在设备中的 pdf 查看器中打开它:

    活动类

    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" />
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      相关资源
      最近更新 更多