【问题标题】:How to send body text field along with files in Android POST multipart form data如何在 Android POST 多部分表单数据中发送正文文本字段以及文件
【发布时间】:2015-10-08 06:56:43
【问题描述】:

我正在尝试将图像与一些文本字段一起发布到服务器。我正在按照以下代码将文件发布到服务器,并且工作正常。但我无法弄清楚将文本字段发送到哪里。

public class TaskPostData extends AsyncTask<Void, Void, Void> {
    private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);

    protected void onPreExecute() {
        this.dialog.setMessage("Loading...");
        this.dialog.setCancelable(false);
        this.dialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        HttpURLConnection conn = null;
        DataOutputStream dos = null;
        DataInputStream inStream = null;
        String existingFileName = file_path;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;
        String urlString = "http://my/post/url";
        try {
            InputStream fileInputStream = getContentResolver().openInputStream(selectedImage);
            URL url = new URL(urlString);
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
            dos = new DataOutputStream(conn.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + lineEnd);

            dos.writeBytes("Content-Disposition: form-data; name=\"image\";filename=\"" + upload_file_name + "\"" + lineEnd); // uploaded_file_name is the Name of the File to be uploaded
            dos.writeBytes(lineEnd);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }
            dos.writeBytes(lineEnd);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            fileInputStream.close();
            dos.flush();
            dos.close();
        } catch (MalformedURLException ex) {
            Log.e("Debug", "error: " + ex.getMessage(), ex);
        } catch (IOException ioe) {
            Log.e("Debug", "error: " + ioe.getMessage(), ioe);
        }

        try {
            inStream = new DataInputStream(conn.getInputStream());
            String str;
            while ((str = inStream.readLine()) != null) {
                Log.e("Debug", "Server Response " + str);
                reponse_data = str;
            }
            inStream.close();
        } catch (IOException ioex) {
            Log.e("Debug", "error: " + ioex.getMessage(), ioex);
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }
    }
}

【问题讨论】:

  • multipart 的每个部分都在两条边界线之间。所以还要添加一个新的 Content-Disposition 行。
  • @greenapps:请给出一个小示例代码。我有 18 个字段要与一张图片一起发送。
  • 试试吧。在这篇文章中展示你的尝试。
  • 在这里查看答案stackoverflow.com/questions/17320886/…,看看你的输出应该是什么样子。每个参数及其值都在边界之间。
  • @greenapps:非常感谢。终于让它工作了。你最后的评论真的很有帮助。

标签: android http-post multipartform-data


【解决方案1】:
private static final String POST_PARAMS = "userName=Pankaj";

这是我要添加的方法

dos.write(POST_PARAMS.getBytes());

【讨论】:

  • 但是在哪里添加这些行呢?
  • 好问题。 Sanny 的解决方案只能在没有图像的情况下工作。
猜你喜欢
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 2015-01-25
  • 2020-10-04
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多