HttpUrlConnection读取输入流返回“-1”

时间:2015-10-21 14:22:08

标签: java android pdf httpurlconnection

我正在向服务器发送帖子请求。请求成功后,响应会将pdf数据发送到客户端以供下载。我正在尝试使用响应生成pdf文件并将其保存到Android设备。我已经成功创建了请求,空的pdf文件,我甚至收到了成功的回复。虽然,当我调用.getInputStream()时,它返回一个“ -1 ”。结果,while循环退出时出现错误 java.lang.ArrayIndexOutOfBoundsException:length = 4096; regionStart = 0; regionLength = -1

我理解为什么我收到此错误,但我不知道如何获得数据响应。

Options +FollowSymLinks

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    <Files .*>
        Order Deny,Allow
        Deny From All
    </Files>

    # Allow asset folders through
    RewriteRule ^(fuel/modules/(.+)?/assets/(.+)) - [L]

    # Protect application and system files from being viewed
    RewriteRule ^(fuel/install/.+|fuel/crons/.+|fuel/data_backup/.+|fuel/codeigniter/.+|fuel/modules/.+|fuel/application/.+) - [F,L]


    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule .* index.php/$0 [L]

    # Prevents access to dot files (.git, .htaccess) - security.
    RewriteCond %{SCRIPT_FILENAME} -d
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteRule "(^|/)\." - [F]

</IfModule>
Options -Indexes

我收到了正确的回复标题。我在下面列出了它们......

protected Boolean doInBackground(String... params) {
        Log.d("LOGOUT", "CREATING REQUEST");

        boolean successful = false;
        HttpURLConnection connection = null;
        URL downloadURL = null;
        InputStream inputStream = null;
        PrintWriter out= null;
        FileOutputStream fileOutputStream = null;
        File file = null;
        int totalsize;

        try {

            downloadURL = new URL("http://exampleurl.com");

            connection = (HttpURLConnection) downloadURL.openConnection();
            connection.setDoOutput(true);

            String data = "param=" + URLEncoder.encode(params[0], "UTF-8") + "&submit=" + URLEncoder.encode("Submit", "UTF-8");

            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length", "" + data.length());

            String sdCard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
            file = new File(sdCard + File.separator + "test.pdf");
            fileOutputStream = new FileOutputStream(file);

            out = new PrintWriter(connection.getOutputStream());
            out.write(data);
            out.close();

            inputStream = connection.getInputStream();

            int read;
            byte[] buffer = new byte[1024];

            while ((read = inputStream.read(buffer)) != -1) ; {

                Log.d("LOGOUT", "BUFFER: " + read);
                // **The LOGOUT message reads "BUFFER: -1" within Logcat**
                fileOutputStream.write(buffer, 0, read);
            }
            successful = true;

        } catch (MalformedURLException e) {
            Log.d("LOGOUT", "ERROR: " + e);

        } catch (UnsupportedEncodingException e) {
            Log.d("LOGOUT", "ERROR: " + e);

        } catch (FileNotFoundException e) {
            Log.d("LOGOUT", "ERROR: " + e);

        } catch (IOException e) {
            Log.d("LOGOUT", "ERROR: " + e);

        } finally {

            if (inputStream != null) {
                try {
                    Log.d("LOGOUT", "CLOSING INPUTSTREAM");
                    inputStream.close();

                } catch (IOException e) {
                    Log.d("LOGOUT", "ERROR: " + e);
                }

                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                        Log.d("LOGOUT", "CLOSING FILEOUTPUTSTREAM");

                    } catch (IOException e) {
                        Log.d("LOGOUT", "ERROR: " + e);

                    }
                }

            }
            if (connection != null) {
                Log.d("LOGOUT", "CLOSING CONNECTION");
                connection.disconnect();

            }

        }

        return successful;
    }

我知道我发送/接收成功请求/回复的事实,但我无法下载pdf数据。最后,我无法控制服务器。我正在与第三方应用程序进行交互。

完成任务我做错了什么?如果不是这样,有没有人知道我可以在哪里解决问题?任何指导都表示赞赏。

0 个答案:

没有答案